List of usage examples for org.apache.poi.xssf.usermodel XSSFWorkbook write
@SuppressWarnings("resource") public final void write(OutputStream stream) throws IOException
From source file:com.photon.phresco.framework.commons.FrameworkUtil.java
License:Apache License
private void writeTestCasesToXLSX(String testSuiteName, String[] cellValue, String status, int numCol, int cellno, CellStyle[] tryStyle, StringBuilder sb) throws PhrescoException { Iterator<Row> rowIterator; try {/*w w w. j a v a 2 s . c o m*/ FileInputStream myInput = new FileInputStream(sb.toString()); OPCPackage opc = OPCPackage.open(myInput); XSSFWorkbook myWorkBook = new XSSFWorkbook(opc); int numberOfSheets = myWorkBook.getNumberOfSheets(); for (int j = 0; j < numberOfSheets; j++) { XSSFSheet mySheet = myWorkBook.getSheetAt(j); if (mySheet.getSheetName().equals(testSuiteName)) { rowIterator = mySheet.rowIterator(); Row next; for (Cell cell : mySheet.getRow((mySheet.getLastRowNum()) - 2)) { tryStyle[cellno] = cell.getCellStyle(); cellno = cellno + 1; } float totalPass = 0; float totalFail = 0; float totalNotApp = 0; float totalBlocked = 0; float notExecuted = 0; float totalTestCases = 0; for (int i = 0; i <= 22; i++) { rowIterator.next(); } do { next = rowIterator.next(); if (StringUtils.isNotEmpty(getValue(next.getCell(1))) && !getValue(next.getCell(0)).equalsIgnoreCase("S.NO")) { String value = getValue(next.getCell(11)); if (StringUtils.isNotEmpty(value)) { if (value.equalsIgnoreCase("success")) { totalPass = totalPass + 1; } else if (value.equalsIgnoreCase("failure")) { totalFail = totalFail + 1; } else if (value.equalsIgnoreCase("notApplicable")) { totalNotApp = totalNotApp + 1; } else if (value.equalsIgnoreCase("blocked")) { totalBlocked = totalBlocked + 1; } } else { notExecuted = notExecuted + 1; } } } while (rowIterator.hasNext()); //to update the status in the index page if (status.equalsIgnoreCase("success")) { totalPass = totalPass + 1; } else if (status.equalsIgnoreCase("failure")) { totalFail = totalFail + 1; } else if (status.equalsIgnoreCase("notApplicable")) { totalNotApp = totalNotApp + 1; } else if (status.equalsIgnoreCase("blocked")) { totalBlocked = totalBlocked + 1; } else { notExecuted = notExecuted + 1; } totalTestCases = totalPass + totalFail + totalNotApp + totalBlocked + notExecuted; XSSFSheet mySheet1 = myWorkBook.getSheetAt(0); rowIterator = mySheet1.rowIterator(); for (int i = 0; i <= 2; i++) { rowIterator.next(); } while (rowIterator.hasNext()) { Row next1 = rowIterator.next(); if (StringUtils.isNotEmpty(getValue(next1.getCell(2))) && !getValue(next1.getCell(2)).equalsIgnoreCase("Total")) { TestSuite createObject = createObject(next1); if (createObject.getName().equals(testSuiteName)) { addCalculationsToIndex(totalPass, totalFail, totalNotApp, totalBlocked, notExecuted, totalTestCases, next1); } } } Row r = null; if (mySheet.getSheetName().equalsIgnoreCase("Index")) { r = mySheet.createRow(next.getRowNum() - 1); } else { r = mySheet.createRow(next.getRowNum() + 1); } for (int i = 0; i < numCol; i++) { Cell cell = r.createCell(i); cell.setCellValue(cellValue[i]); cell.setCellStyle(tryStyle[i]); } FileOutputStream fileOut = new FileOutputStream(sb.toString()); myWorkBook.write(fileOut); myInput.close(); fileOut.close(); } } } catch (PhrescoException e) { throw new PhrescoException(e); } catch (IOException e) { throw new PhrescoException(e); } catch (InvalidFormatException e) { throw new PhrescoException(e); } }
From source file:com.respam.comniq.models.POIexcelExporter.java
License:Open Source License
public void createFile() throws IOException { String path = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output"; File file = new File(path + File.separator + "POImovieInfo.xlsx"); // Blank Workbook XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Movies"); // Data for Labels Map<String, Object[]> label = new TreeMap<>(); label.put("1", new Object[] { "Poster", "Title", "Release Date", "Metascore", "IMDB Rating", "Plot", "IMDB URL", "Genre", "Director", "Actors", "Rating", "Runtime" }); // Iterate over label and write to sheet Set<String> keyset = label.keySet(); // Setting Style for the Label Row Font font = workbook.createFont(); font.setFontHeight((short) 240); font.setFontName("Courier New"); font.setBold(true);//from w ww .j a v a2s .com XSSFCellStyle labelStyle = workbook.createCellStyle(); labelStyle.setWrapText(true); labelStyle.setFont(font); // Setting column widths sheet.setColumnWidth(0, 4000); sheet.setColumnWidth(1, 8500); sheet.setColumnWidth(2, 4000); sheet.setColumnWidth(3, 4000); sheet.setColumnWidth(4, 3500); sheet.setColumnWidth(5, 9500); sheet.setColumnWidth(6, 5000); sheet.setColumnWidth(7, 4000); sheet.setColumnWidth(8, 3500); sheet.setColumnWidth(9, 4000); sheet.setColumnWidth(10, 3000); sheet.setColumnWidth(11, 4000); // Freezing the first row sheet.createFreezePane(0, 1); // Filling each cell with Label data for (String key : keyset) { Row row = sheet.createRow(0); Object[] objArr = label.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); cell.setCellStyle(labelStyle); cell.setCellValue((String) obj); } } // Writing the excel file try { FileOutputStream out = new FileOutputStream(file); workbook.write(out); out.close(); System.out.println("Excel File Created"); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.respam.comniq.models.POIexcelExporter.java
License:Open Source License
public void excelWriter(JSONObject parsedObj, int rownum) throws IOException { String path = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output"; File file = new File(path + File.separator + "POImovieInfo.xlsx"); String thumbnailPath = System.getProperty("user.home") + File.separator + "comniq" + File.separator + "output" + File.separator + "thumbnails"; File posterFile = new File(thumbnailPath + File.separator + parsedObj.get("Title") + ".jpg"); if (!file.exists()) { createFile();/*from w w w . j av a 2 s . c o m*/ } if (file.exists() && checked.equals(false)) { findLastRow(); } try { FileInputStream fis = new FileInputStream(file); XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet sheet = workbook.getSheet("Movies"); Map<String, Object[]> label = new TreeMap<>(); label.put("1", new Object[] { "", parsedObj.get("Title"), parsedObj.get("Released"), parsedObj.get("Metascore"), parsedObj.get("imdbRating"), parsedObj.get("Plot"), parsedObj.get("imdbID"), parsedObj.get("Genre"), parsedObj.get("Director"), parsedObj.get("Actors"), parsedObj.get("Rated"), parsedObj.get("Runtime") }); Set<String> keyset = label.keySet(); // Setting Style for the Label Row XSSFCellStyle contentStyle = workbook.createCellStyle(); contentStyle.setWrapText(true); contentStyle.setVerticalAlignment(VerticalAlignment.TOP); rownum = rownum + lastRow; if (posterFile.exists()) { InputStream imageStream = new FileInputStream( thumbnailPath + File.separator + parsedObj.get("Title") + ".jpg"); byte[] imageBytes = IOUtils.toByteArray(imageStream); pictureureIdx = workbook.addPicture(imageBytes, Workbook.PICTURE_TYPE_PNG); imageStream.close(); CreationHelper helper = workbook.getCreationHelper(); drawing = sheet.createDrawingPatriarch(); anchor = helper.createClientAnchor(); } for (String key : keyset) { Row row = sheet.createRow(rownum++); row.setHeight((short) 2000); Object[] objArr = label.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); cell.setCellStyle(contentStyle); cell.setCellValue((String) obj); } if (posterFile.exists()) { anchor.setCol1(0); anchor.setRow1(rownum - 1); anchor.setCol2(0); anchor.setRow2(rownum - 1); Picture pict = drawing.createPicture(anchor, pictureureIdx); pict.resize(1, 1); } } FileOutputStream out = new FileOutputStream(file); workbook.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.saba.CalendarDemo.java
License:Apache License
public static void main(String[] args) throws Exception { Map<String, Object> data = new HashMap<String, Object>(); prepareXLSDynamicValues(data);/*from ww w.j a v a 2 s . co m*/ XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Awarded Bid & Contact Details"); Map<String, CellStyle> styles = createStyles(workbook); sheet.setPrintGridlines(false); sheet.setDisplayGridlines(false); PrintSetup printSetup = sheet.getPrintSetup(); printSetup.setLandscape(true); sheet.setFitToPage(true); sheet.setHorizontallyCenter(true); setupColumnWidthForEachFields(sheet); //preparing the contact & details table along with data prepareContactDetailsTableAndData(data, sheet, styles); int contactdetrow = contactDetails.length + 2; //preparing the award bid & details table along with data prepareAwardBidDetailsTableAndData(data, sheet, styles, contactdetrow); int awardDetailsRow = (contactDetails.length + awardedBidDetails.length + 4); //preparing the product & details table prepareProductDetailsTable(workbook, sheet, styles, awardDetailsRow); //preparing the product & details table data prepareProductDetailsTableData(data, sheet, styles, awardDetailsRow); FileOutputStream out = new FileOutputStream("award_bid.xlsx"); workbook.write(out); out.close(); }
From source file:com.safeway.app.appcert.util.smoketester.TestCaseReader.java
public void writetoExcel(List<TestScriptTemplate> testcases) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Smoke Test Result"); DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); //create header first Row rowhead = sheet.createRow(2);//from w ww . jav a 2 s . com rowhead.createCell(0).setCellValue("Application Code"); rowhead.createCell(1).setCellValue("Test Case #1: Login to application"); rowhead.createCell(2).setCellValue("Test Case #2: Verify Home Page title and element"); rowhead.createCell(3).setCellValue("Test Case #3: Verify next page title and element"); rowhead.createCell(4).setCellValue("Date and Time Log"); rowhead.createCell(5).setCellValue("Test Execution Log"); Iterator i = testcases.iterator(); int rownum = 3; while (i.hasNext()) { TestScriptTemplate testresult = (TestScriptTemplate) i.next(); Row row = sheet.createRow(rownum); rownum++; Date date = new Date(); row.createCell(0).setCellValue(testresult.getAppCode()); row.createCell(1).setCellValue(testresult.getTc1_resultSummary()); row.createCell(2).setCellValue(testresult.getTc2_resultSummary()); row.createCell(3).setCellValue(testresult.getTc3_resultSummary()); //row.createCell(4).getCellStyle().setWrapText(true); row.createCell(4).setCellValue(dateFormat.format(date)); row.createCell(5).getCellStyle().setWrapText(true); row.createCell(5).setCellValue(testresult.getLogs()); } try { Date date = new Date(); DateFormat dateFormatFile1 = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss"); FileOutputStream out = new FileOutputStream( new File("C:\\Nino\\SmokeTest_" + dateFormatFile1.format(date) + ".xlsx")); workbook.write(out); out.close(); System.out.println("Excel written successfully.."); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.sec.ose.osi.report.ReportGenerator.java
License:Open Source License
/** * @param projectsInfo/*from w w w. ja v a 2s . com*/ * @param sourceExcelFilename * @param targeExcelFilename * @param creatorName * @param creatorEmail * @param organizationName * @param observer */ public void generateIdentifyReport(ArrayList<ProjectInfoForIdentifyReport> projectsInfo, String sourceExcelFilename, String targeExcelFilename, String creatorName, String creatorEmail, String organizationName, UIResponseObserver observer) { if (observer == null) observer = new DefaultUIResponseObserver(); SheetsManager reportSheetManager = new SheetsManager(); String ProjectName = projectsInfo.get(0).getProjectName(); for (int i = 1; i < projectsInfo.size(); i++) { ProjectInfoForIdentifyReport pInfo = projectsInfo.get(i); ProjectName += "\n" + pInfo.getProjectName(); } XSSFWorkbook wb = reportSheetManager.createWorkbook(); try { observer.setMessageHeader("Creating report cover \n"); // 1. create cover sheet reportSheetManager.createCoverSheet(ProjectName, creatorName, creatorEmail, organizationName); // 2. create Summary Sheet SummaryRowGenerator summaryRowGen = new SummaryRowGenerator(projectsInfo, observer); reportSheetManager.writeSummaryRow(summaryRowGen.createSummaryRow()); // 3. create Bill of Materials Row ArrayList<IdentifiedFilesRow> allProjectIdentifiedFilesRowList = new ArrayList<IdentifiedFilesRow>(); for (ProjectInfoForIdentifyReport projectInfo : projectsInfo) { BillOfMaterialsRowGenerator billOfMaterialsRowGen = new BillOfMaterialsRowGenerator(); ArrayList<BillOfMaterialsRow> BillOfMaterialsRowList = billOfMaterialsRowGen .createBillOfMaterialsRowList(projectInfo, observer); ArrayList<LicenseSummaryRow> licenseSummaryRowList = billOfMaterialsRowGen .getLicenseSummaryRowList(); ArrayList<IdentifiedFilesRow> tmpIdentifiedFilesRow = billOfMaterialsRowGen .getIdentifiedFilesRowList(); if (tmpIdentifiedFilesRow != null) allProjectIdentifiedFilesRowList.addAll(tmpIdentifiedFilesRow); reportSheetManager.writeBillOfMaterialsRow(projectInfo.getProjectName(), BillOfMaterialsRowList, licenseSummaryRowList); } // 4. create Identified Files Row reportSheetManager.writeIdentifiedFilesRow(allProjectIdentifiedFilesRowList); // 5. write file FileOutputStream fileOut = new FileOutputStream(targeExcelFilename); wb.write(fileOut); fileOut.close(); observer.setSuccessMessage("\"" + targeExcelFilename + "\" has been created successfully.\n"); } catch (Exception e) { observer.setFailMessage("Can not create report. \n"); e.printStackTrace(); } }
From source file:com.smanempat.controller.ControllerClassification.java
public void convertToExcel(JTable tableResult, File fileToSave) throws FileNotFoundException, IOException { XSSFWorkbook workBook = new XSSFWorkbook(); XSSFSheet sheet = workBook.createSheet(); XSSFRow row;//from ww w. j a v a2s . c o m int rowCountData = tableResult.getRowCount(); Map<Integer, Object[]> data = new HashMap<Integer, Object[]>(); data.put(1, new Object[] { "NIS", "NAMA", "JK", "UN", "PT BINDO", "PT MTK", "PT BING", "PT IPA", "MINAT", "JURUSAN" }); for (int i = 0; i < rowCountData; i++) { // System.out.println(tableResult.getValueAt(i, 1)); data.put((i + 2), new Object[] { tableResult.getValueAt(i, 0), tableResult.getValueAt(i, 1), tableResult.getValueAt(i, 2), tableResult.getValueAt(i, 3), tableResult.getValueAt(i, 4), tableResult.getValueAt(i, 5), tableResult.getValueAt(i, 6), tableResult.getValueAt(i, 7), tableResult.getValueAt(i, 8), tableResult.getValueAt(i, 9) }); } //System.out.println("KeySet " + data.keySet()); Set<Integer> keyID = data.keySet(); int rowID = 0; for (Integer key : keyID) { row = sheet.createRow(rowID); Object[] tempData = data.get(key); rowID++; int cellID = 0; for (Object obj : tempData) { Cell cell = row.createCell(cellID); cell.setCellValue(obj.toString()); cellID++; } } FileOutputStream out = new FileOutputStream(fileToSave); workBook.write(out); out.close(); System.out.println(fileToSave + " Berhasil disimpan"); }
From source file:com.smanempat.view.CreateWorkbook.java
public static void main(String[] args) throws FileNotFoundException, IOException { //Create blank workbook XSSFWorkbook workbook = new XSSFWorkbook(); //Create file system using spesific name File namaWorkbook = new File("D://WorkBook.xlsx"); FileOutputStream out = new FileOutputStream(namaWorkbook); //Write operation workbook workbook.write(out); out.close();/* w w w . j a v a 2s. co m*/ System.out.println("File workbook berhasil dibuat!"); }
From source file:com.springapp.mvc.CreatePivotTable.java
License:Apache License
public static void main(String[] args) throws FileNotFoundException, IOException, InvalidFormatException { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = (XSSFSheet) wb.createSheet(); XSSFSheet sheet2 = (XSSFSheet) wb.createSheet(); //Create some data to build the pivot table on setCellData(sheet2);//from w ww. j av a 2 s .co m XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A1:D5"), new CellReference("A1"), sheet2); //Configure the pivot table //Use first column as row label pivotTable.addRowLabel(0); pivotTable.addRowLabel(3); //pivotTable.addDataColumn(0, false); //pivotTable.addRowLabel(1); //pivotTable.addRowLabel(2); //pivotTable.addRowLabel(3); //pivotTable.addRowLabel(3); //pivotTable.addDataColumn(1, true); //pivotTable.addDataColumn(1, true); //Sum up the second column pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1); //Set the third column as filter //pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2); //Add filter on forth column //pivotTable.addReportFilter(3); //pivotTable.addReportFilter(0); //pivotTable.addReportFilter(0); //pivotTable.addRowLabel(0); //pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1); System.out.println(pivotTable.getRowLabelColumns()); FileOutputStream fileOut = new FileOutputStream("ooxml-pivottable.xlsx"); wb.write(fileOut); fileOut.close(); }
From source file:com.svi.main.logic.ExtractAndPrint.java
private void writeLogFile(List<Nodes> nodesHolder) { DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HHmm"); Date date = new Date(); Sheet mainSheet;//from w w w.j a v a 2 s . co m Sheet dataSheet; Sheet elementSheet; Row dataSheetRow; Row elementSheetRow; InputStream fis; XSSFWorkbook workbook; File outputPath = new File(logPath + "\\Logs"); File outputFile = new File(logPath + "\\Logs\\BPO KPI Report " + dateFormat.format(date) + ".xlsx"); // File name int dataSheetRowCount = 1; int elementSheetRowCount = 1; int totalElementException = 0; try { if (!Files.exists(outputPath.toPath())) { Files.createDirectories(outputPath.toPath()); } fis = ExtractAndPrint.class.getResourceAsStream("bpo_template.xlsx"); workbook = new XSSFWorkbook(fis); //Style for exception sheet XSSFCellStyle style = workbook.createCellStyle(); style.setFillBackgroundColor(IndexedColors.YELLOW.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); //Get data Sheet mainSheet = workbook.getSheetAt(0); writeProjectDetails(mainSheet); dataSheet = workbook.getSheetAt(4); dataSheetRow = dataSheet.createRow(0); elementSheet = workbook.getSheetAt(1); elementSheetRow = elementSheet.createRow(0); //Write excel headers writeDataSheetHeaders(dataSheetRow); writeElementSheetHeaders(elementSheetRow); //Set progress bar values progress = new AtomicInteger(0); total = new AtomicInteger(nodesHolder.size()); mf.setJprogressValues(total, progress); // Sort the nodes per Node ID Collections.sort(nodesHolder, new Comparator<Nodes>() { public int compare(Nodes o1, Nodes o2) { return o1.getNodeId().compareTo(o2.getNodeId()); } }); //Write Data Sheet for (Nodes node : nodesHolder) { mf.loader(); dataSheetRow = dataSheet.createRow(dataSheetRowCount++); writeDataSheet(node, dataSheetRow); } for (Nodes node : nodesHolder) { for (Elements e : node) { if ((e.getStatus().equalsIgnoreCase("COMPLETE") || e.getStatus().equalsIgnoreCase("PROCESSING")) && e.getTotalProcTime() > MAX_MINUTES_ELEMENT) { totalElementException++; } } } progress = new AtomicInteger(0); total = new AtomicInteger(totalElementException); mf.setJprogressValues(total, progress); //Write exception sheet for (Nodes node : nodesHolder) { for (Elements e : node) { if ((e.getStatus().equalsIgnoreCase("COMPLETE") || e.getStatus().equalsIgnoreCase("PROCESSING")) && e.getTotalProcTime() > MAX_MINUTES_ELEMENT) { elementSheetRow = elementSheet.createRow(elementSheetRowCount++); writeElementSheet(e, elementSheetRow); mf.elementLoader(); } } } XSSFFormulaEvaluator.evaluateAllFormulaCells((XSSFWorkbook) workbook); try (FileOutputStream outputStream = new FileOutputStream(outputFile)) { workbook.write(outputStream);//Write the Excel File outputStream.close(); } workbook.close(); fis.close(); mf.generateMessage(); } catch (Exception ex) { Logger.getLogger(ExtractAndPrint.class.getName()).log(Level.SEVERE, null, ex); } }