List of usage examples for org.apache.poi.ss.usermodel Workbook createSheet
Sheet createSheet();
From source file:ch.dbs.actions.reports.KbartReport.java
License:Open Source License
private HSSFWorkbook getXLSContent(final Konto k) { final Text cn = new Text(); final Workbook wb = new HSSFWorkbook(); final Sheet s = wb.createSheet(); try {//ww w .j a va 2 s. c o m // add header for XLS initXLS(wb, s); // internal holdings are visible final List<Bestand> stock = new Bestand().getAllKontoBestand(k.getId(), true, cn.getConnection()); // transform to KbartForm final KbartForm kbf = new KbartForm(); final List<KbartForm> kbarts = kbf.createKbartForms(stock, cn.getConnection()); short rowNr = 0; for (final KbartForm kbart : kbarts) { rowNr++; // add holdings getXLSLine(wb, s, kbart, rowNr); } // adjust all columns in size short columnNr = 0; while (columnNr < 16) { s.autoSizeColumn(columnNr); columnNr++; } } finally { cn.close(); } return (HSSFWorkbook) wb; }
From source file:cherry.goods.excel.ExcelFactory.java
License:Apache License
/** * ???<br />/*from w ww . j a v a 2 s. com*/ * * @param workbook ??Excel * @return ???Excel */ private static Workbook createSheet(Workbook workbook) { workbook.createSheet(); return workbook; }
From source file:cn.mypandora.util.MyExcelUtil.java
License:Apache License
/** * Excel, ?// w w w . j a v a 2 s . c om * * @param filepath ? * @param sheetTitle Sheet?? * @param fieldTitles Sheet???? */ public static void writeExcel(String filepath, String sheetTitle, String fieldTitles) { Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() }; for (int i = 0; i < wbs.length; i++) { Workbook wb = wbs[i]; // ExcelSheet Sheet sheet = wb.createSheet(); wb.setSheetName(0, sheetTitle); // Sheet createTitle(sheet, fieldTitles); // ?Excel saveExcelFile(wb, filepath); } }
From source file:cn.mypandora.util.MyExcelUtil.java
License:Apache License
/** * Excle/* w w w. ja va 2 s . c o m*/ * * @param filepath ? * @param sheetTitle Sheet?? * @param fieldTitles Sheet???? * @param objList ?? * @param objClass ??? * @param fieldNames ?objClassfield?? */ public static void writeExcel(String filepath, String sheetTitle, String fieldTitles, List<?> objList, Class<?> objClass, String fieldNames) { Workbook[] wbs = new Workbook[] { new HSSFWorkbook(), new XSSFWorkbook() }; for (int j = 0; j < wbs.length; j++) { Workbook workbook = wbs[j]; CreationHelper creationHelper = workbook.getCreationHelper(); Sheet sheet = workbook.createSheet(); workbook.setSheetName(0, sheetTitle); createTitle(sheet, fieldTitles); createBody(sheet, objList, objClass, fieldNames); // ?Excel saveExcelFile(workbook, filepath); } }
From source file:com.actelion.research.spiritapp.ui.util.POIUtils.java
License:Open Source License
@SuppressWarnings("rawtypes") public static void exportToExcel(String[][] table, ExportMode exportMode) throws IOException { Class[] types = getTypes(table); Workbook wb = new XSSFWorkbook(); Map<String, CellStyle> styles = new HashMap<String, CellStyle>(); CellStyle style;/*w ww .j a v a2s . c o m*/ DataFormat df = wb.createDataFormat(); Font font = wb.createFont(); font.setFontName("Serif"); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 15); style = wb.createCellStyle(); style.setFont(font); styles.put("title", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 10); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setFont(font); style.setWrapText(true); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("th", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_LEFT); style.setFont(font); style.setWrapText(true); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderTop(CellStyle.BORDER_THIN); style.setTopBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_LEFT); style.setFont(font); style.setWrapText(true); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-border", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-double", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-right", style); font = wb.createFont(); font.setFontName("Serif"); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-bold", style); font = wb.createFont(); font.setFontName("Serif"); font.setFontHeightInPoints((short) 9); style = wb.createCellStyle(); style.setBorderRight(CellStyle.BORDER_THIN); style.setRightBorderColor(IndexedColors.BLACK.getIndex()); style.setBorderLeft(CellStyle.BORDER_THIN); style.setLeftBorderColor(IndexedColors.BLACK.getIndex()); style.setAlignment(CellStyle.ALIGN_RIGHT); style.setFont(font); style.setDataFormat(df.getFormat("d.mm.yyyy h:MM")); style.setVerticalAlignment(CellStyle.VERTICAL_TOP); styles.put("td-date", style); Sheet sheet = wb.createSheet(); sheet.setFitToPage(true); Cell cell; int maxRows = 0; for (int r = 0; r < table.length; r++) { Row row = sheet.createRow(r); if (r == 0) { row.setRowStyle(styles.get("th")); } int rows = 1; for (int c = 0; c < table[r].length; c++) { cell = row.createCell(c); String s = table[r][c]; if (s == null) continue; rows = Math.max(rows, s.split("\n").length); try { if (exportMode == ExportMode.HEADERS_TOP && r == 0) { cell.setCellStyle(styles.get("th")); cell.setCellValue(s); } else if (exportMode == ExportMode.HEADERS_TOPLEFT && (r == 0 || c == 0)) { if (r == 0 && c == 0) { cell.setCellStyle(styles.get("td")); } else { cell.setCellStyle(styles.get("th")); } cell.setCellValue(s); } else if (types[c] == Double.class) { cell.setCellStyle(styles.get("td-double")); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(Double.parseDouble(s)); } else if (types[c] == String.class) { cell.setCellStyle( styles.get(exportMode == ExportMode.HEADERS_TOPLEFT ? "td-border" : "td")); cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue(s); } else { cell.setCellStyle(styles.get("td-right")); cell.setCellValue(s); } } catch (Exception e) { cell.setCellStyle(styles.get("td")); cell.setCellValue(s); } } maxRows = Math.max(maxRows, rows); row.setHeightInPoints(rows * 16f); } // Add footer notes if (footerData.size() > 0) { Row row = sheet.createRow(table.length); row.setHeightInPoints((footerData.size() * sheet.getDefaultRowHeightInPoints())); cell = row.createCell(0); sheet.addMergedRegion(new CellRangeAddress(row.getRowNum(), //first row (0-based) row.getRowNum(), //last row (0-based) 0, //first column (0-based) table[0].length - 1 //last column (0-based) )); //for ( String data : footerData ) { style = wb.createCellStyle(); style.setWrapText(true); cell.setCellStyle(style); cell.setCellValue(MiscUtils.flatten(footerData, "\n")); //} } footerData.clear(); autoSizeColumns(sheet); if (table.length > 0) { for (int c = 0; c < table[0].length; c++) { if (sheet.getColumnWidth(c) > 10000) sheet.setColumnWidth(c, 3000); } } if (exportMode == ExportMode.HEADERS_TOPLEFT) { for (int r = 1; r < table.length; r++) { sheet.getRow(r).setHeightInPoints(maxRows * 16f); } } File reportFile = IOUtils.createTempFile("export_", ".xlsx"); FileOutputStream out = new FileOutputStream(reportFile); wb.write(out); wb.close(); out.close(); Desktop.getDesktop().open(reportFile); }
From source file:com.admin.poi.ExcelUtils.java
License:Apache License
/** * excel//from w w w .j a v a 2 s . c om * * @param excelSheet sheet ? * @param workbook * @param outputStream ? */ private static void export(ExcelSheet excelSheet, Workbook workbook, OutputStream outputStream) throws IOException, InvocationTargetException, IllegalAccessException { Sheet sheet; sheet = workbook.createSheet(); workbook.setSheetOrder(sheet.getSheetName(), excelSheet.getSheetIndex()); // write head writeHead(excelSheet, sheet); // sheet int writeRowIndex = excelSheet.getStartRowIndex(); if (excelSheet.getDataList() != null && !excelSheet.getDataList().isEmpty()) { for (Object rowData : excelSheet.getDataList()) { // proc row Row row = Optional.ofNullable(sheet.getRow(writeRowIndex)).orElse(sheet.createRow(writeRowIndex)); writeRow(excelSheet, row, rowData); writeRowIndex++; } } workbook.write(outputStream); }
From source file:com.bizosys.dataservice.dao.ReadXLS.java
License:Apache License
@Override protected List<String> populate() throws SQLException { checkCondition();//from w w w. j a v a 2 s .co m Workbook workbook = getWorkbook(); Sheet sheet = workbook.createSheet(); ResultSetMetaData md = rs.getMetaData(); int totalCol = md.getColumnCount(); String[] cols = createLabels(md, totalCol); try { if (null != templateFile) { File templateFileObject = new File(templateFile); if (templateFileObject.exists()) { Workbook templateWorkbook = new HSSFWorkbook(new FileInputStream(templateFileObject)); Sheet templatesheet = templateWorkbook.getSheetAt(0); Iterator<Row> rowIterator = templatesheet.iterator(); while (rowIterator.hasNext()) { Row templateRow = rowIterator.next(); Row row = sheet.createRow(startRowIndex++); Iterator<Cell> cellIterator = templateRow.cellIterator(); while (cellIterator.hasNext()) { Cell templateCell = cellIterator.next(); Cell cell = row.createCell(templateCell.getColumnIndex()); cell.setCellType(templateCell.getCellType()); switch (templateCell.getCellType()) { case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: cell.setCellValue(templateCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: cell.setCellValue(templateCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: cell.setCellValue(templateCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: cell.setCellValue(templateCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: cell.setCellValue(templateCell.getStringCellValue()); break; } } } } else { System.err.println("Can not read " + templateFileObject.getAbsolutePath()); } } while (this.rs.next()) { createRecord(totalCol, cols, sheet); } workbook.write(out); } catch (IOException ex) { throw new SQLException(ex); } return null; }
From source file:com.bizosys.dataservice.dao.WriteToXls.java
License:Apache License
public void write(List<Object[]> records) throws Exception { Workbook workbook = getWorkbook(); Sheet sheet = workbook.createSheet(); if (null != templateFile) { File templateFileObject = new File(templateFile); if (templateFileObject.exists()) { Workbook templateWorkbook = new HSSFWorkbook(new FileInputStream(templateFileObject)); Sheet templatesheet = templateWorkbook.getSheetAt(0); Iterator<Row> rowIterator = templatesheet.iterator(); while (rowIterator.hasNext()) { Row templateRow = rowIterator.next(); Row row = sheet.createRow(startRowIndex++); Iterator<Cell> cellIterator = templateRow.cellIterator(); while (cellIterator.hasNext()) { Cell templateCell = cellIterator.next(); Cell cell = row.createCell(templateCell.getColumnIndex()); cell.setCellType(templateCell.getCellType()); switch (templateCell.getCellType()) { case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_BOOLEAN: cell.setCellValue(templateCell.getBooleanCellValue()); break; case Cell.CELL_TYPE_ERROR: cell.setCellValue(templateCell.getErrorCellValue()); break; case Cell.CELL_TYPE_FORMULA: cell.setCellValue(templateCell.getCellFormula()); break; case Cell.CELL_TYPE_NUMERIC: cell.setCellValue(templateCell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: cell.setCellValue(templateCell.getStringCellValue()); break; }/*from ww w .j a v a 2 s . com*/ } } } else { System.err.println("Can not read " + templateFileObject.getAbsolutePath()); } } for (Object[] cols : records) { createRecord(cols, sheet); } workbook.write(out); }
From source file:com.cisco.ca.cstg.pdi.utils.Util.java
public static void json2Excel(OutputStream output, String json, String[] properties, String[] columnsNames) throws IOException { Workbook workbook = new XSSFWorkbook(); Sheet sheet = workbook.createSheet(); Row header = sheet.createRow(0);//from w w w . j a v a 2 s . co m for (int i = 0; i < columnsNames.length; i++) { String string = columnsNames[i]; Cell cell = header.createCell(i); RichTextString text = new XSSFRichTextString(string); cell.setCellValue(text); } LOGGER.info("Writing on workbook."); try { ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(json); int i = 0; for (JsonNode jsonNode2 : jsonNode) { Row row = sheet.createRow(++i); for (int j = 0; j < properties.length; j++) { String string = properties[j]; Cell cell = row.createCell(j); RichTextString text = new XSSFRichTextString(jsonNode2.get(string).getTextValue()); cell.setCellValue(text); } } } catch (JsonProcessingException e) { LOGGER.error(e.getMessage(), e); } workbook.write(output); workbook.close(); LOGGER.info("Written on workbook."); }
From source file:com.evidon.areweprivateyet.Aggregator.java
License:Open Source License
public void createSpreadSheet() throws Exception { int row = 2, cell = 0, sheet = 0; FileOutputStream file = new FileOutputStream(path + "analysis.xls"); Workbook wb = new HSSFWorkbook(); // content: total content length sheet. Sheet s = wb.createSheet(); wb.setSheetName(sheet, "Content Length"); this.createHeader(wb, s, "Total Content Length in MB", 0); Row r = s.createRow(row);/*from www . java2 s . co m*/ for (String database : results.keySet()) { Cell c = r.createCell(cell); c.setCellValue(results.get(database).totalContentLength / 1024 / 1024); cell++; } row++; cell = 0; r = s.createRow(row); for (String database : results.keySet()) { Cell c = r.createCell(cell); if (database.equals("baseline")) { c.setCellValue("Decrease:"); Map<String, String> contents = new LinkedHashMap<String, String>(); contents.put(s.getSheetName(), "0"); decrease.put(database, contents); } else { c = r.createCell(cell); c.setCellType(Cell.CELL_TYPE_FORMULA); c.setCellFormula("ROUND((100-(" + getCellLetter(cell - 1) + "3*100/A3)),0)"); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); evaluator.evaluateFormulaCell(c); Map<String, String> contents = new LinkedHashMap<String, String>(); contents.put(s.getSheetName(), c.getNumericCellValue() + ""); decrease.put(database, contents); } cell++; } sheet++; // When content is created, baseline is used as a base for every entry. For example, // if baseline contained doubleclick.com, this will be output and each other analyzer's // map, like ghosterys analyzer is then asked for the content's mapping for doubleclick. // So, if baseline does not contain blah.com, yet ghostery map does, this entry is never // shown in the spreadsheet or any other results. // so this means if we have tracker/whatever URLs in a non-baseline profile // and these URLs are NOT in the baseline profile, // we wouldn't see those trackers/whatever in the final comparison. // content: HTTP Requests s = wb.createSheet(); wb.setSheetName(sheet, "HTTP Requests"); this.createHeader(wb, s, "Pages with One or More HTTP Requests to the Public Suffix", 1); this.createContent(wb, s, "requestCountPerDomain"); sheet++; // content: HTTP Set-Cookie Responses s = wb.createSheet(); wb.setSheetName(sheet, "HTTP Set-Cookie Responses"); this.createHeader(wb, s, "Pages with One or More HTTP Responses from the Public Suffix That Include a Set-Cookie Header", 1); this.createContent(wb, s, "setCookieResponses"); sheet++; // content: Cookie Added - Cookie Deleted s = wb.createSheet(); wb.setSheetName(sheet, "Cookies Added-Deleted"); this.createHeader(wb, s, "Cookies Added - Cookies Deleted Per Domain", 1); this.createContent(wb, s, "cookieTotals"); sheet++; // content: Local Storage counts per domain s = wb.createSheet(); wb.setSheetName(sheet, "Local Storage"); this.createHeader(wb, s, "Local Storage counts per domain", 1); this.createContent(wb, s, "localStorageContents"); sheet++; // content: Pretty Chart s = wb.createSheet(); wb.setSheetName(sheet, "Overall"); int rownum = 0, cellnum = 0; // Header r = s.createRow(rownum); Cell c = r.createCell(0); s.setColumnWidth(0, 8000); c.setCellValue( "Overall effectiveness measured by percentage of decrease vs baseline (0 for any negative effect)"); rownum++; r = s.createRow(rownum); cellnum++; for (String database : decrease.keySet()) { if (database.equals("baseline")) { continue; } c = r.createCell(cellnum); c.setCellValue(database); CellStyle cs = wb.createCellStyle(); Font f = wb.createFont(); f.setBoldweight(Font.BOLDWEIGHT_BOLD); cs.setFont(f); c.setCellStyle(cs); cellnum++; } CellStyle numberStyle = wb.createCellStyle(); numberStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("number")); // Content for (String type : decrease.get("baseline").keySet()) { cellnum = 0; rownum++; r = s.createRow(rownum); c = r.createCell(cellnum); c.setCellValue(type); cellnum++; for (String database : decrease.keySet()) { if (database.equals("baseline")) { continue; } c = r.createCell(cellnum); c.setCellStyle(numberStyle); double decreaseValue = Double.parseDouble(decrease.get(database).get(type)); if (decreaseValue < 0) decreaseValue = 0; c.setCellValue(decreaseValue); cellnum++; } } /* for (String database : decrease.keySet()) { for (String type : decrease.get(database).keySet()) { System.out.println(database + "|" + type + "|" + decrease.get(database).get(type)); } } */ wb.write(file); file.close(); }