List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet createTable
@Deprecated @Removal(version = "4.2.0") public XSSFTable createTable()
From source file:CreateTable.java
License:Apache License
public static void main(String[] args) throws FileNotFoundException, IOException { Workbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) wb.createSheet(); //Create // w w w .j av a2 s . c o m XSSFTable table = sheet.createTable(); table.setDisplayName("Test"); CTTable cttable = table.getCTTable(); //Style configurations CTTableStyleInfo style = cttable.addNewTableStyleInfo(); style.setName("TableStyleMedium2"); style.setShowColumnStripes(false); style.setShowRowStripes(true); //Set which area the table should be placed in AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(3, 3)); cttable.setRef(reference.formatAsString()); cttable.setId(1); cttable.setName("Test"); cttable.setTotalsRowCount(1); CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount(3); CTTableColumn column; XSSFRow row; XSSFCell cell; for (int i = 0; i < 3; i++) { //Create column column = columns.addNewTableColumn(); column.setName("Column"); column.setId(i + 1); //Create row row = sheet.createRow(i); for (int j = 0; j < 3; j++) { //Create cell cell = row.createCell(j); if (i == 0) { cell.setCellValue("Column" + j); } else { cell.setCellValue(i + j + 0.0); } } } FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx"); wb.write(fileOut); fileOut.close(); }
From source file:de.symeda.sormas.api.doc.DataDictionaryGenerator.java
License:Open Source License
@SuppressWarnings("unchecked") private XSSFSheet createEntitySheet(XSSFWorkbook workbook, Class<? extends EntityDto> entityClass, String i18nPrefix) {/*from www . j ava2s . com*/ String name = I18nProperties.getCaption(i18nPrefix); String safeName = WorkbookUtil.createSafeSheetName(name); XSSFSheet sheet = workbook.createSheet(safeName); // Create XSSFTable table = sheet.createTable(); String safeTableName = safeName.replaceAll("\\s", "_"); table.setName(safeTableName); table.setDisplayName(safeTableName); XssfHelper.styleTable(table, 1); int columnCount = EntityColumn.values().length; int rowNumber = 0; // header XSSFRow headerRow = sheet.createRow(rowNumber++); for (EntityColumn column : EntityColumn.values()) { table.addColumn(); String columnCaption = column.toString(); columnCaption = columnCaption.substring(0, 1) + columnCaption.substring(1).toLowerCase(); headerRow.createCell(column.ordinal()).setCellValue(columnCaption); } // column width sheet.setColumnWidth(EntityColumn.FIELD.ordinal(), 256 * 30); sheet.setColumnWidth(EntityColumn.TYPE.ordinal(), 256 * 30); sheet.setColumnWidth(EntityColumn.CAPTION.ordinal(), 256 * 30); sheet.setColumnWidth(EntityColumn.DESCRIPTION.ordinal(), 256 * 60); sheet.setColumnWidth(EntityColumn.REQUIRED.ordinal(), 256 * 10); sheet.setColumnWidth(EntityColumn.DISEASES.ordinal(), 256 * 45); sheet.setColumnWidth(EntityColumn.OUTBREAKS.ordinal(), 256 * 10); CellStyle defaultCellStyle = workbook.createCellStyle(); defaultCellStyle.setWrapText(true); List<Class<Enum<?>>> usedEnums = new ArrayList<Class<Enum<?>>>(); for (Field field : entityClass.getDeclaredFields()) { if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) continue; XSSFRow row = sheet.createRow(rowNumber++); // field name XSSFCell fieldNameCell = row.createCell(EntityColumn.FIELD.ordinal()); fieldNameCell.setCellValue(field.getName()); // value range XSSFCell fieldValueCell = row.createCell(EntityColumn.TYPE.ordinal()); fieldValueCell.setCellStyle(defaultCellStyle); Class<?> fieldType = field.getType(); if (fieldType.isEnum()) { // use enum type name - values are added below // Object[] enumValues = fieldType.getEnumConstants(); // StringBuilder valuesString = new StringBuilder(); // for (Object enumValue : enumValues) { // if (valuesString.length() > 0) // valuesString.append(", "); // valuesString.append(((Enum) enumValue).name()); // } // fieldValueCell.setCellValue(valuesString.toString()); fieldValueCell.setCellValue(fieldType.getSimpleName()); if (!usedEnums.contains(fieldType)) { usedEnums.add((Class<Enum<?>>) fieldType); } } else if (EntityDto.class.isAssignableFrom(fieldType)) { fieldValueCell.setCellValue(fieldType.getSimpleName().replaceAll("Dto", "")); } else if (ReferenceDto.class.isAssignableFrom(fieldType)) { fieldValueCell.setCellValue(fieldType.getSimpleName().replaceAll("Dto", "")); } else if (String.class.isAssignableFrom(fieldType)) { fieldValueCell.setCellValue(I18nProperties.getCaption("text")); } else if (Date.class.isAssignableFrom(fieldType)) { fieldValueCell.setCellValue(I18nProperties.getCaption("date")); } else if (Number.class.isAssignableFrom(fieldType)) { fieldValueCell.setCellValue(I18nProperties.getCaption("number")); } else if (Boolean.class.isAssignableFrom(fieldType) || boolean.class.isAssignableFrom(fieldType)) { fieldValueCell.setCellValue(Boolean.TRUE.toString() + ", " + Boolean.FALSE.toString()); } // caption XSSFCell captionCell = row.createCell(EntityColumn.CAPTION.ordinal()); captionCell.setCellValue(I18nProperties.getPrefixCaption(i18nPrefix, field.getName(), "")); // description XSSFCell descriptionCell = row.createCell(EntityColumn.DESCRIPTION.ordinal()); descriptionCell.setCellStyle(defaultCellStyle); descriptionCell.setCellValue(I18nProperties.getPrefixDescription(i18nPrefix, field.getName(), "")); // required XSSFCell requiredCell = row.createCell(EntityColumn.REQUIRED.ordinal()); if (field.getAnnotation(Required.class) != null) requiredCell.setCellValue(true); // diseases XSSFCell diseasesCell = row.createCell(EntityColumn.DISEASES.ordinal()); diseasesCell.setCellStyle(defaultCellStyle); Diseases diseases = field.getAnnotation(Diseases.class); if (diseases != null) { StringBuilder diseasesString = new StringBuilder(); for (Disease disease : diseases.value()) { if (diseasesString.length() > 0) diseasesString.append(", "); diseasesString.append(disease.toShortString()); } diseasesCell.setCellValue(diseasesString.toString()); } else { diseasesCell.setCellValue("All"); } // outbreak XSSFCell outbreakCell = row.createCell(EntityColumn.OUTBREAKS.ordinal()); if (field.getAnnotation(Outbreaks.class) != null) outbreakCell.setCellValue(true); } AreaReference reference = workbook.getCreationHelper().createAreaReference(new CellReference(0, 0), new CellReference(rowNumber - 1, columnCount - 1)); table.setCellReferences(reference); table.getCTTable().addNewAutoFilter(); for (Class<Enum<?>> usedEnum : usedEnums) { rowNumber = createEnumTable(sheet, rowNumber + 1, usedEnum); } return sheet; }
From source file:de.symeda.sormas.api.doc.DataDictionaryGenerator.java
License:Open Source License
private int createEnumTable(XSSFSheet sheet, int startRow, Class<Enum<?>> enumType) { // Create//from w w w .j a v a 2 s. c o m XSSFTable table = sheet.createTable(); String safeTableName = (sheet.getSheetName() + enumType.getSimpleName()).replaceAll("\\s", "_"); table.setName(safeTableName); table.setDisplayName(safeTableName); XssfHelper.styleTable(table, 2); int columnCount = EnumColumn.values().length; int rowNumber = startRow; // header XSSFRow headerRow = sheet.createRow(rowNumber++); for (EnumColumn column : EnumColumn.values()) { table.addColumn(); String columnCaption = column.toString(); columnCaption = columnCaption.substring(0, 1) + columnCaption.substring(1).toLowerCase(); headerRow.createCell(column.ordinal()).setCellValue(columnCaption); } Object[] enumValues = enumType.getEnumConstants(); for (Object enumValueObject : enumValues) { XSSFRow row = sheet.createRow(rowNumber++); XSSFCell cell; Enum<?> enumValue = ((Enum<?>) enumValueObject); cell = row.createCell(EnumColumn.TYPE.ordinal()); if (enumValueObject == enumValues[0]) { cell.setCellValue(enumType.getSimpleName()); } cell = row.createCell(EnumColumn.VALUE.ordinal()); cell.setCellValue(enumValue.name()); cell = row.createCell(EnumColumn.CAPTION.ordinal()); String caption = enumValue.toString(); cell.setCellValue(caption); cell = row.createCell(EnumColumn.DESCRIPTION.ordinal()); String desc = I18nProperties.getEnumDescription(enumValue); cell.setCellValue(DataHelper.equal(caption, desc) ? "" : desc); cell = row.createCell(EnumColumn.SHORT.ordinal()); String shortCaption = I18nProperties.getEnumCaptionShort(enumValue); cell.setCellValue(DataHelper.equal(caption, shortCaption) ? "" : shortCaption); } AreaReference reference = new AreaReference(new CellReference(startRow, 0), new CellReference(rowNumber - 1, columnCount - 1), SpreadsheetVersion.EXCEL2007); table.setCellReferences(reference); table.getCTTable().addNewAutoFilter(); return rowNumber; }
From source file:edu.cmu.emfta.actions.CutSet.java
License:Open Source License
public XSSFWorkbook toSingleSheetWorkbook() { XSSFWorkbook workbook = new XSSFWorkbook(); int cutSetIdentifier = 0; double cutsetProbability; XSSFSheet sheet = workbook.createSheet(); XSSFTable table = sheet.createTable(); table.setDisplayName("Cutsets"); CTTable cttable = table.getCTTable(); // Set which area the table should be placed in AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2)); cttable.setRef(reference.formatAsString()); cttable.setId((long) 1); cttable.setName("Cutsets"); cttable.setTotalsRowCount((long) 1); CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount((long) 3); CTTableColumn column;//ww w.ja v a2 s . co m XSSFRow row; XSSFCell cell; column = columns.addNewTableColumn(); // Create row row = sheet.createRow(0); CellStyle headingCellStyle = workbook.createCellStyle(); XSSFFont headingFont = workbook.createFont(); headingFont.setBold(true); headingCellStyle.setFont(headingFont); row.setRowStyle(headingCellStyle); CellStyle normalCellStyle = workbook.createCellStyle(); XSSFFont normalFont = workbook.createFont(); normalFont.setBold(false); normalCellStyle.setFont(normalFont); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue("Identifier"); break; } case 1: { cell.setCellValue("Description"); break; } case 2: { cell.setCellValue("Probability"); break; } } } int rowId = 1; for (List<Event> events : cutset) { row = sheet.createRow(rowId++); row = sheet.createRow(rowId++); row.setRowStyle(normalCellStyle); cell = row.createCell(0); cell.setCellValue("Cutset #" + cutSetIdentifier); cutsetProbability = 1; for (int i = 0; i < events.size(); i++) { cutsetProbability = cutsetProbability * events.get(i).getProbability(); } cell = row.createCell(2); if (cutsetProbability != 1) { cell.setCellValue("" + cutsetProbability); } else { cell.setCellValue("" + cutsetProbability); } // System.out.println("[CutSet] cutset id=" + cutSetIdentifier); for (int i = 0; i < events.size(); i++) { Event e = events.get(i); // System.out.println("[CutSet] event name=" + e.getName()); // Create row row = sheet.createRow(rowId++); row.setRowStyle(normalCellStyle); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue(e.getName()); break; } case 1: { cell.setCellValue(e.getDescription()); break; } case 2: { cell.setCellValue(e.getProbability()); break; } } } } cutSetIdentifier = cutSetIdentifier + 1; } return workbook; }
From source file:edu.cmu.emfta.actions.CutSet.java
License:Open Source License
public XSSFWorkbook toMultiSheetsWorkbook() { XSSFWorkbook workbook = new XSSFWorkbook(); int cutSetIdentifier = 0; double cutsetProbability; for (List<Event> events : cutset) { cutsetProbability = 1;/*www .j a v a2s. co m*/ for (int i = 0; i < events.size(); i++) { cutsetProbability = cutsetProbability * events.get(i).getProbability(); } // System.out.println("[CutSet] cutset id=" + cutSetIdentifier); XSSFSheet sheet = workbook.createSheet(); XSSFTable table = sheet.createTable(); table.setDisplayName("Cutset"); CTTable cttable = table.getCTTable(); // Set which area the table should be placed in AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2)); cttable.setRef(reference.formatAsString()); cttable.setId((long) 1); cttable.setName("Cutset " + cutSetIdentifier); cttable.setTotalsRowCount((long) 1); CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount((long) 3); CTTableColumn column; XSSFRow row; XSSFCell cell; column = columns.addNewTableColumn(); // Create row row = sheet.createRow(0); CellStyle headingCellStyle = workbook.createCellStyle(); XSSFFont headingFont = workbook.createFont(); headingFont.setBold(true); headingCellStyle.setFont(headingFont); row.setRowStyle(headingCellStyle); CellStyle normalCellStyle = workbook.createCellStyle(); XSSFFont normalFont = workbook.createFont(); normalFont.setBold(false); normalCellStyle.setFont(normalFont); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue("Identifier"); break; } case 1: { cell.setCellValue("Description"); break; } case 2: { if (cutsetProbability == 1) { cell.setCellValue("Probability"); } else { cell.setCellValue("Probability (" + cutsetProbability + ")"); } break; } } } for (int i = 0; i < events.size(); i++) { Event e = events.get(i); System.out.println("[CutSet] event name=" + e.getName()); // Create column column = columns.addNewTableColumn(); column.setName("Column"); column.setId((long) i + 1); // Create row row = sheet.createRow(i + 1); row.setRowStyle(normalCellStyle); for (int j = 0; j < 3; j++) { // Create cell cell = row.createCell(j); switch (j) { case 0: { cell.setCellValue(e.getName()); break; } case 1: { cell.setCellValue(e.getDescription()); break; } case 2: { cell.setCellValue(e.getProbability()); break; } } } } cutSetIdentifier = cutSetIdentifier + 1; } return workbook; }
From source file:packtest.CreateTable.java
License:Apache License
public static void main(String[] args) throws FileNotFoundException, IOException { Workbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) wb.createSheet(); //Create /* w w w. j a v a2 s .c o m*/ XSSFTable table = sheet.createTable(); table.setDisplayName("Test"); CTTable cttable = table.getCTTable(); //Style configurations CTTableStyleInfo style = cttable.addNewTableStyleInfo(); style.setName("TableStyleMedium2"); style.setShowColumnStripes(false); style.setShowRowStripes(true); //Set which area the table should be placed in AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2)); cttable.setRef(reference.formatAsString()); cttable.setId(1); cttable.setName("Test"); cttable.setTotalsRowCount(1); CTTableColumns columns = cttable.addNewTableColumns(); columns.setCount(3); CTTableColumn column; XSSFRow row; XSSFCell cell; for (int i = 0; i < 3; i++) { //Create column column = columns.addNewTableColumn(); column.setName("Column"); column.setId(i + 1); //Create row row = sheet.createRow(i); for (int j = 0; j < 3; j++) { //Create cell cell = row.createCell(j); if (i == 0) { cell.setCellValue("Column" + j); } else { cell.setCellValue("0"); } } } FileOutputStream fileOut = new FileOutputStream(Utils.getPath("ooxml-table.xlsx")); wb.write(fileOut); fileOut.close(); }