List of usage examples for org.apache.poi.xssf.usermodel XSSFTable getCTTable
@Internal(since = "POI 3.15 beta 3") public CTTable getCTTable()
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 /*from www . jav a 2s . co 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:ch.bfh.lca._15h.library.export.ExportToExcel.java
/*** * Prototype function. Not yet functional. * Allow to generate an age pyramid graphic in Excel by using an existing Excel Template. * @param language Language of the label in the Excel file * @param rows Arrays of rows to include in the listing * @param excelFilePath Path of the outputed file * @throws FileNotFoundException/* ww w . j a v a 2 s .c om*/ * @throws IOException * @throws InvalidFormatException */ public static void exportToAgePyramid(Translation.TRANSLATION_LANGUAGE language, GenericResultRow[] rows, String excelFilePath) throws FileNotFoundException, IOException, InvalidFormatException { //open template URL url = Translation.class.getClassLoader() .getResource("ch/bfh/lca/_15h/library/export/template/alter-pyramide-v2.xlsx"); //Workbook wb = WorkbookFactory.create(new File(url.getPath())); XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(new File(url.getPath()))); Sheet sheet = wb.getSheetAt(0); //http://www.programming-free.com/2012/12/create-charts-in-excel-using-java.html //https://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook Row row; Cell cell; for (int i = 0; i < 20; i++) { row = sheet.getRow(i + 1); if (row == null) { row = sheet.createRow(i + 1); } for (int j = 0; j < 3; j++) { cell = row.getCell(j); if (cell == null) { cell = row.createCell(j); } switch (j) { case 0: cell.setCellValue(i); break; case 1: cell.setCellValue(i * j * -1); break; case 2: cell.setCellValue(i * j); break; } } } //redefine data range //http://thinktibits.blogspot.ch/2014/09/Excel-Insert-Format-Table-Apache-POI-Example.html XSSFSheet sheet1 = wb.getSheetAt(0); XSSFTable table = sheet1.getTables().get(0); CTTable cttable = table.getCTTable(); AreaReference my_data_range = new AreaReference(new CellReference(0, 0), new CellReference(20, 2)); /* Set Range to the Table */ cttable.setRef(my_data_range.formatAsString()); // cttable.setDisplayName("DATEN"); /* this is the display name of the table */ //cttable.setName("test"); /* This maps to "displayName" attribute in <table>, OOXML */ //cttable.setId(1L); //id attribute against table as long value /* //redefine data range Name rangeCell = wb.getName("DATEN"); //Set new range for named range //String reference = sheetName + "!$C$" + (deface + 1) + ":$C$" + (rowNum + deface); String reference = sheet.getSheetName() + "!$A$2:$C$20"; //Assigns range value to named range rangeCell.setRefersToFormula(reference); */ //write to the file FileOutputStream fileOut = new FileOutputStream(excelFilePath); wb.write(fileOut); fileOut.close(); wb.close(); }
From source file:Controller.Sonstiges.CreateExcelContorller.java
public void createTableExcelAction() { try {//from w w w .j a v a 2s . c om Workbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) wb.createSheet(); this.sheet = sheet; //Create XSSFTable table = this.sheet.createTable(); table.setDisplayName("StudentsTablle"); CTTable cttable = table.getCTTable(); //Style configurations CTTableStyleInfo style = cttable.addNewTableStyleInfo(); style.setName("inprotucTable"); style.setShowColumnStripes(false); style.setShowRowStripes(true); //Set which area the table should be placed in cttable.setId(1); cttable.setName("Test"); cttable.setTotalsRowCount(1); CTTableColumns columns = cttable.addNewTableColumns(); this.columns = columns; this.columns.setCount(3); // save the data in Excel Tabele before that u create an excel File this.writeInTableExcelAction(); //Create a File Excel FileOutputStream fileOut = new FileOutputStream("/home/kourda/Downloads/table.xls"); wb.write(fileOut); fileOut.close(); } catch (FileNotFoundException f) { System.out.print(f.toString()); } catch (IOException io) { System.out.print(io.toString()); } }
From source file:Controller.Sonstiges.ExcelController.java
public void createExcelDatei() throws JSONException { try {/*from ww w . java2s . c o m*/ this.wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) this.wb.createSheet(); this.sheet = sheet; //Create XSSFTable table = this.sheet.createTable(); table.setDisplayName("StudentsTablle"); CTTable cttable = table.getCTTable(); this.makeStyle(); //Set which area the table should be placed i cttable.setId(1); CTTableColumns columns = cttable.addNewTableColumns(); this.columns = columns; this.makeExcelDatei(); //Create a File Excel this.createDirectory(); this.createFileName(); FileOutputStream fileOut = new FileOutputStream(this.pahtFile); this.wb.write(fileOut); fileOut.close(); Desktop dt = Desktop.getDesktop(); dt.open(new File(this.pahtFile)); } catch (FileNotFoundException f) { System.out.print(f.toString()); } catch (IOException io) { System.out.print(io.toString()); } }
From source file:de.jlo.talendcomp.excel.SpreadsheetOutput.java
License:Apache License
private boolean extendTable(XSSFTable table, int firstRow, int firstCol, int lastRow) throws Exception { try {/*from w w w .ja v a2 s . c o m*/ AreaReference currentRef = null; if (currentType == SpreadsheetTyp.XLS) { currentRef = new AreaReference(table.getCTTable().getRef(), SpreadsheetVersion.EXCEL97); } else { currentRef = new AreaReference(table.getCTTable().getRef(), SpreadsheetVersion.EXCEL2007); } CellReference topLeft = currentRef.getFirstCell(); CellReference buttomRight = currentRef.getLastCell(); if (topLeft.getRow() <= firstRow && buttomRight.getRow() >= firstRow && topLeft.getCol() <= firstCol && buttomRight.getCol() >= firstCol) { // this table is within our write area, we have to expand it AreaReference newRef = new AreaReference(topLeft, // left top including the header line new CellReference(lastRow, buttomRight.getCol())); // bottom right table.getCTTable().setRef(newRef.formatAsString()); return true; } else { return false; } } catch (Exception t) { if (workbook instanceof SXSSFWorkbook) { throw new Exception( "Extending table ranges cannot work in a workbook which is not fully loaded because of the memory saving mode. Uncheck Memory saving mode in tFileExcelWorkbookOpen!", t); } else { throw t; } } }
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 w w w .ja v a 2 s . c om*/ 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// ww w . j a v a2 s. c om 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:de.symeda.sormas.api.doc.XssfHelper.java
License:Open Source License
public static void styleTable(XSSFTable table, int styleNumber) { // Style the table - can this be simplified? table.getCTTable().addNewTableStyleInfo(); String tableStyleName = "TableStyleLight" + styleNumber; table.getCTTable().getTableStyleInfo().setName(tableStyleName); XSSFTableStyleInfo style = (XSSFTableStyleInfo) table.getStyle(); style.setName(tableStyleName);/* ww w . j a v a 2 s. c o m*/ style.setFirstColumn(false); style.setLastColumn(false); style.setShowRowStripes(true); style.setShowColumnStripes(false); }
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;//from ww w .jav a2 s .com 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;//from w w w .java2 s .c o 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; }