List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook write
private void write(POIFSFileSystem fs) throws IOException
From source file:com.photon.phresco.framework.commons.FrameworkUtil.java
License:Apache License
public boolean deleteTestCasesFromXls(String filePath, String testSuiteName, String testCaseId) { boolean hasTrue = false; List<TestCase> testCases = new ArrayList<TestCase>(); TestCase tstCase = new TestCase(); Iterator<Row> rowIterator; try {/*from w ww . j a v a 2 s . c o m*/ FileInputStream myInput = new FileInputStream(filePath); HSSFWorkbook myWorkBook = new HSSFWorkbook(myInput); int numberOfSheets = myWorkBook.getNumberOfSheets(); for (int j = 0; j < numberOfSheets; j++) { HSSFSheet mySheet = myWorkBook.getSheetAt(j); if (mySheet.getSheetName().equals(testSuiteName)) { rowIterator = mySheet.rowIterator(); for (int i = 0; i <= 23; i++) { rowIterator.next(); } while (rowIterator.hasNext()) { Row next = rowIterator.next(); if (testCaseId.equalsIgnoreCase(getValue(next.getCell(3)))) { tstCase = readTest(next); mySheet.removeRow(next); int rowNum = next.getRowNum(); int newNum = rowNum + 1; HSSFRow row = mySheet.getRow(newNum); if (row != null) { mySheet.shiftRows(newNum, mySheet.getLastRowNum(), -1); } hasTrue = true; break; } } } } if (hasTrue) { for (int j = 0; j < numberOfSheets; j++) { HSSFSheet myHSSFSheet = myWorkBook.getSheetAt(j); if (myHSSFSheet.getSheetName().equals(testSuiteName)) { rowIterator = myHSSFSheet.rowIterator(); for (int i = 0; i <= 23; i++) { rowIterator.next(); } while (rowIterator.hasNext()) { Row next = rowIterator.next(); TestCase createObject = readTest(next); if (StringUtils.isNotEmpty(createObject.getTestCaseId())) { testCases.add(createObject); } } float totalPass = 0; float totalFail = 0; float totalNotApplicable = 0; float totalBlocked = 0; int totalTestCases = testCases.size(); for (TestCase testCase : testCases) { String testCaseStatus = testCase.getStatus(); if (testCaseStatus.equalsIgnoreCase("success")) { totalPass = totalPass + 1; } else if (testCaseStatus.equalsIgnoreCase("failure")) { totalFail = totalFail + 1; } else if (testCaseStatus.equalsIgnoreCase("notApplicable")) { totalNotApplicable = totalNotApplicable + 1; } else if (testCaseStatus.equalsIgnoreCase("blocked")) { totalBlocked = totalBlocked + 1; } } // if(tstCase.getStatus().equalsIgnoreCase("success")) { // totalPass = totalPass - 1; // } else if (tstCase.getStatus().equalsIgnoreCase("failure")) { // totalFail = totalFail - 1; // } else if (tstCase.getStatus().equalsIgnoreCase("notApplicable")) { // totalNotApplicable = totalNotApplicable - 1; // } else if (tstCase.getStatus().equalsIgnoreCase("blocked")) { // totalBlocked = totalBlocked - 1; // } HSSFSheet 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 (StringUtils.isNotEmpty(tstCase.getTestCaseId()) && createObject.getName().equals(testSuiteName)) { updateIndex(totalPass, totalFail, totalNotApplicable, totalBlocked, next1, totalTestCases, "delete"); } } } } } myInput.close(); FileOutputStream outFile = new FileOutputStream(filePath); myWorkBook.write(outFile); outFile.close(); } } catch (Exception e) { } return hasTrue; }
From source file:com.photon.phresco.framework.commons.FrameworkUtil.java
License:Apache License
private List<TestCase> readTestCase(String filePath, String fileName, com.photon.phresco.commons.model.TestCase tstCase) throws PhrescoException { List<TestCase> testCases = new ArrayList<TestCase>(); try {// w ww . j a v a2s. co m File testDir = new File(filePath); StringBuilder sb = new StringBuilder(filePath); if (testDir.isDirectory()) { FilenameFilter filter = new PhrescoFileFilter("", "xlsx"); File[] listFiles = testDir.listFiles(filter); if (listFiles.length != 0) { for (File file1 : listFiles) { if (file1.isFile()) { sb.append(File.separator); sb.append(file1.getName()); break; } } updateTestCaseToXLSX(fileName, tstCase, testCases, sb); } else { FilenameFilter filter1 = new PhrescoFileFilter("", "xls"); File[] listFiles1 = testDir.listFiles(filter1); if (listFiles1.length != 0) { for (File file2 : listFiles1) { if (file2.isFile()) { sb.append(File.separator); sb.append(file2.getName()); break; } } FileInputStream myInput = new FileInputStream(sb.toString()); HSSFWorkbook myWorkBook = new HSSFWorkbook(myInput); int numberOfSheets = myWorkBook.getNumberOfSheets(); for (int j = 0; j < numberOfSheets; j++) { HSSFSheet mySheet = myWorkBook.getSheetAt(j); if (mySheet.getSheetName().equals(fileName)) { readTestFromSheet(tstCase, testCases, mySheet); if (StringUtils.isNotEmpty(tstCase.getTestCaseId())) { updateIndexPage(fileName, tstCase, testCases, myWorkBook); } if (StringUtils.isNotEmpty(tstCase.getTestCaseId())) { myInput.close(); FileOutputStream outFile = new FileOutputStream(sb.toString()); myWorkBook.write(outFile); outFile.close(); } } } } else { FilenameFilter odsFilter = new PhrescoFileFilter("", "ods"); File[] odsListFiles = testDir.listFiles(odsFilter); for (File file2 : odsListFiles) { if (file2.isFile()) { sb.append(File.separator); sb.append(file2.getName()); break; } } testCases = readTestCasesFormODS(sb, testCases, fileName, tstCase); } } } } catch (Exception e) { } return testCases; }
From source file:com.photon.phresco.framework.commons.FrameworkUtil.java
License:Apache License
private void writeTestCaseToXLS(String testSuiteName, String[] cellValue, String status, int numCol, int cellno, CellStyle[] tryStyle, StringBuilder sb) throws PhrescoException { Iterator<Row> rowIterator; try {/* ww w.j ava 2 s . c o m*/ FileInputStream myInput = new FileInputStream(sb.toString()); HSSFWorkbook myWorkBook = new HSSFWorkbook(myInput); int numberOfSheets = myWorkBook.getNumberOfSheets(); for (int j = 0; j < numberOfSheets; j++) { HSSFSheet myHssfSheet = myWorkBook.getSheetAt(j); if (myHssfSheet.getSheetName().equals(testSuiteName)) { rowIterator = myHssfSheet.rowIterator(); Row next; for (Cell cell : myHssfSheet.getRow((myHssfSheet.getLastRowNum()) - 1)) { tryStyle[cellno] = cell.getCellStyle(); cellno = cellno + 1; if (cellno == 15) { break; } } 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; HSSFSheet mySheetHssf = myWorkBook.getSheetAt(0); rowIterator = mySheetHssf.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 (myHssfSheet.getSheetName().equalsIgnoreCase("Index")) { r = myHssfSheet.createRow(next.getRowNum() - 1); } else { r = myHssfSheet.createRow(next.getRowNum() + 1); } for (int i = 0; i < numCol; i++) { Cell cell = r.createCell(i); cell.setCellValue(cellValue[i]); if (tryStyle[i] != null) { 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 (FileNotFoundException e) { throw new PhrescoException(e); } catch (IOException e) { throw new PhrescoException(e); } }
From source file:com.primovision.lutransport.core.util.TollCompanyTagUploadUtil.java
private static InputStream createInputStream(HSSFWorkbook wb) { //dumpToFile(wb); ByteArrayOutputStream out = new ByteArrayOutputStream(); try {/*from w w w . j a v a 2s. c om*/ wb.write(out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } InputStream targetStream = new ByteArrayInputStream(out.toByteArray()); return targetStream; }
From source file:com.primovision.lutransport.core.util.TollCompanyTagUploadUtil.java
private static ByteArrayOutputStream createOutputStream(HSSFWorkbook wb) { //dumpToFile(wb); ByteArrayOutputStream out = new ByteArrayOutputStream(); try {/*from ww w .ja va 2 s . c o m*/ wb.write(out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return out; }
From source file:com.primovision.lutransport.core.util.TollCompanyTagUploadUtil.java
private static void dumpToFile(HSSFWorkbook wb) { FileOutputStream fOut;/*from ww w . j a v a 2 s .c o m*/ try { fOut = new FileOutputStream("/Users/raghav/Desktop/Test.xls"); wb.write(fOut); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.project.jsica.cdi.ReporteBean.java
public void reporte2(List<ReportePermisoBean> reporte) { LOG.info("TAMAO reporte: " + reporte.size()); FacesContext fc = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); HSSFWorkbook libro = new HSSFWorkbook(); HSSFFont fuente = libro.createFont(); fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle estiloCeldaCabecera = libro.createCellStyle(); estiloCeldaCabecera.setFont(fuente); estiloCeldaCabecera.setAlignment(HSSFCellStyle.ALIGN_CENTER); DataFormat format = libro.createDataFormat(); HSSFCellStyle style = libro.createCellStyle(); style.setDataFormat(format.getFormat("hh:mm:ss")); HSSFCellStyle fechas = libro.createCellStyle(); fechas.setDataFormat(format.getFormat("dd.MM.yyyy")); HSSFSheet hoja = libro.createSheet("hoja 1"); //CREAR LAS CABECERAS String[] cabeceras = { "CODIGO", "NOMBRE", "HORA INICIO", "HORA FIN", "HORAS", "MINUTOS", "FECHA", "MOTIVO" }; HSSFRow filaCabecera = hoja.createRow(0); for (int x = 0; x < cabeceras.length; x++) { HSSFCell cabecera = filaCabecera.createCell(x); cabecera.setCellValue(cabeceras[x]); cabecera.setCellStyle(estiloCeldaCabecera); }/*from w w w . j av a 2 s. c o m*/ //FIN DE CABECERAS for (int i = 0; i < reporte.size(); i++) { HSSFRow fila = hoja.createRow(i + 1); HSSFCell columna1 = fila.createCell(0); columna1.setCellValue(reporte.get(i).getCodigo()); HSSFCell columna2 = fila.createCell(1); columna2.setCellValue(reporte.get(i).getNombre()); HSSFCell columna3 = fila.createCell(2); columna3.setCellValue(reporte.get(i).getHoraInicio()); columna3.setCellStyle(style); HSSFCell columna4 = fila.createCell(3); columna4.setCellValue(reporte.get(i).getHoraFin()); columna4.setCellStyle(style); HSSFCell columna5 = fila.createCell(4); columna5.setCellValue(reporte.get(i).getHoras()); HSSFCell columna6 = fila.createCell(5); columna6.setCellValue(reporte.get(i).getMinutos()); HSSFCell columna7 = fila.createCell(6); columna7.setCellValue(reporte.get(i).getFechaReal()); columna7.setCellStyle(fechas); HSSFCell columna8 = fila.createCell(7); columna8.setCellValue(reporte.get(i).getMotivo()); } try { OutputStream output = response.getOutputStream(); libro.write(output); output.close(); fc.responseComplete(); } catch (IOException ex) { LOG.info("ERROR: " + ex); } }
From source file:com.project.jsica.cdi.ReporteBean.java
public void reporte3() { if (nuevoReporte) { LOG.info("OPCION: " + opcionReporte); String nombreReporte = ""; int filas = 0; if (opcionReporte == 2) { reporte = registroAsistenciaController.buscarXArea(areaSeleccionada, desde, hasta); LOG.info("TAMAO reporte: " + reporte.size()); nombreReporte = "Reporte de asistencia por area"; filas = 1;/*from w ww . j av a 2 s .com*/ } else if (opcionReporte == 1) { reporte = registroAsistenciaController.buscarXEmpleado(empleado, desde, hasta); LOG.info("TAMAO reporte: " + reporte.size()); nombreReporte = "Reporte de asistencia por empleado"; filas = 0; } FacesContext fc = FacesContext.getCurrentInstance(); HttpServletResponse response = (HttpServletResponse) fc.getExternalContext().getResponse(); response.reset(); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=" + nombreReporte + ".xls"); HSSFWorkbook libro = new HSSFWorkbook(); HSSFFont fuente = libro.createFont(); fuente.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle estiloCeldaCabecera = libro.createCellStyle(); estiloCeldaCabecera.setFont(fuente); estiloCeldaCabecera.setAlignment(HSSFCellStyle.ALIGN_CENTER); DataFormat format = libro.createDataFormat(); HSSFCellStyle style = libro.createCellStyle(); style.setDataFormat(format.getFormat("hh:mm:ss")); HSSFCellStyle fechas = libro.createCellStyle(); fechas.setDataFormat(format.getFormat("dd.MM.yyyy")); HSSFSheet hoja = libro.createSheet("Reporte de Asistencias"); //CREAR LAS CABECERAS String[] cabeceras = { "CODIGO", "APELLIDOS Y NOMBRES", "FECHA", "TIPO", "HORA DE INGRESO", "HORA DE SALIDA", "MARCACION DE ENTRADA", "MARCACION DE SALIDA", "TARDANZA(Minutos)", "SALIDA REFRIGERIO", "ENTRADA REFRIGERIO", "MARCACION SALIDA R", "MARCACION DE ENTRADA R", "TARDANZA(refrigerio)", "TARDANZA TOTAL" }; if (filas == 1) { HSSFRow filaArea = hoja.createRow(0); HSSFCell Area = filaArea.createCell(0); Area.setCellValue("AREA"); Area.setCellStyle(estiloCeldaCabecera); HSSFCell nombre = filaArea.createCell(1); nombre.setCellValue(areaSeleccionada.getNombre() + ""); } HSSFRow filaCabecera = hoja.createRow(filas); for (int x = 0; x < cabeceras.length; x++) { HSSFCell cabecera = filaCabecera.createCell(x); cabecera.setCellValue(cabeceras[x]); cabecera.setCellStyle(estiloCeldaCabecera); } //FIN DE CABECERAS for (int i = filas; i < reporte.size(); i++) { HSSFRow fila = hoja.createRow(i + 1); HSSFCell columna1 = fila.createCell(0); columna1.setCellValue(reporte.get(i).getEmpleado().getCodigo()); HSSFCell columna2 = fila.createCell(1); columna2.setCellValue(reporte.get(i).getEmpleado().getNombreCompleto()); HSSFCell columna3 = fila.createCell(2); columna3.setCellValue(reporte.get(i).getFecha()); columna3.setCellStyle(fechas); HSSFCell columna4 = fila.createCell(3); columna4.setCellValue(reporte.get(i).getTipo() + ""); HSSFCell columna5 = fila.createCell(4); columna5.setCellValue(reporte.get(i).getHoraEntrada()); columna5.setCellStyle(style); HSSFCell columna6 = fila.createCell(5); columna6.setCellValue(reporte.get(i).getHoraSalida()); columna6.setCellStyle(style); HSSFCell columna7 = fila.createCell(6); if (reporte.get(i).getMarcacionInicio() != null) { columna7.setCellValue(reporte.get(i).getMarcacionInicio()); columna7.setCellStyle(style); } else { columna7.setCellValue("No marco."); } HSSFCell columna8 = fila.createCell(7); if (reporte.get(i).getMarcacionFin() != null) { columna8.setCellValue(reporte.get(i).getMarcacionFin()); columna8.setCellStyle(style); } else { columna8.setCellValue("No marco."); } HSSFCell columna9 = fila.createCell(8); int minutos = (int) ((reporte.get(i).getMilisTardanzaTotal() / (1000 * 60)) % 60); columna9.setCellValue(minutos); HSSFCell columna10 = fila.createCell(9); columna10.setCellValue(reporte.get(i).getHoraSalidaRefrigerio()); columna10.setCellStyle(style); HSSFCell columna11 = fila.createCell(10); columna11.setCellValue(reporte.get(i).getHoraEntradaRefrigerio()); columna11.setCellStyle(style); HSSFCell columna12 = fila.createCell(11); if (reporte.get(i).getMarcacionInicioRefrigerio() != null) { columna12.setCellValue(reporte.get(i).getMarcacionInicioRefrigerio()); columna12.setCellStyle(style); } else { columna12.setCellValue("No marco."); } HSSFCell columna13 = fila.createCell(12); if (reporte.get(i).getMarcacionFinRefrigerio() != null) { columna13.setCellValue(reporte.get(i).getMarcacionFinRefrigerio()); columna13.setCellStyle(style); } else { columna13.setCellValue("No marco."); } HSSFCell columna14 = fila.createCell(13); columna14.setCellValue((int) ((reporte.get(i).getMilisTardanzaRefrigerio() / (1000 * 60)) % 60)); HSSFCell columna15 = fila.createCell(14); columna15.setCellValue((int) ((reporte.get(i).getMilisTardanzaTotalFinal() / (1000 * 60)) % 60)); } try { OutputStream output = response.getOutputStream(); libro.write(output); output.close(); fc.responseComplete(); } catch (IOException ex) { LOG.info("ERROR: " + ex); } nuevoReporte = false; } }
From source file:com.pureinfo.dolphin.export.impl.ExcelExporterImpl.java
License:Open Source License
/** * @throws PureException//from w ww . j a va 2 s . co m * @see com.pureinfo.dolphin.export.IExporter#export(OutputStream, * IExportGoods) */ public void export(OutputStream _os, IExportGoods _goods) throws PureException { try { //1. to create sheet HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); workbook.setSheetName(0, _goods.getName(), (short) 1); //2. to export headers if (_goods.hasHeader()) { HSSFCellStyle headerStyle = getHeaderStyle(workbook); exportHeaders(sheet, _goods.getHeaders(), headerStyle); } //3. to export data Object[] values; HSSFCellStyle dateStyle = getDateStyle(workbook); int nRowNum = 1; Iterator iter = _goods.iterator(); while (iter.hasNext()) { values = _goods.unpackGoods(iter.next()); exportRow(sheet, values, nRowNum++, dateStyle); } //4. to output to stream workbook.write(_os); } catch (IOException ex) { throw new PureException(PureException.UNKNOWN, "export " + _goods.getName() + " to excel", ex); } }
From source file:com.pureinfo.srm.project.model.compile.helper.CompileExcelExportHelper.java
License:Open Source License
public void export(OutputStream _os, IExportGoods _goods, int _year) throws PureException { try {/*from w ww. j av a2 s. co m*/ // 1. to create sheet HSSFWorkbook workbook = new HSSFWorkbook(); style = workbook.createCellStyle(); style.setWrapText(true); HSSFSheet sheet = workbook.createSheet(); workbook.setSheetName(0, _goods.getName(), (short) 1); // change the print way to landscape sheet.getPrintSetup().setLandscape(true); // headLine is the true header of the page. // Workbook book = new CompileWorkBook(); // int index = book.addSSTString(HSSFHeader.font("", "Border") + // HSSFHeader.fontSize((short) 16) // + "\u6d59\u6c5f\u5927\u5b66" + _year // + // "\u5e74\u9ad8\u65b0\u529e\u65b0\u4e0a\u9879\u76ee\u8ba1\u5212\u9879\u76ee\u5355",true); HSSFHeader headLine = sheet.getHeader(); headLine.setCenter(HSSFHeader.font("", "Border") + HSSFHeader.fontSize((short) 16) + "" + _year + ""); // footer HSSFFooter footer = sheet.getFooter(); footer.setRight("." + HSSFFooter.page() + "."); footer.setLeft(this.dateFormat(new Date())); // 2. to export headers if (_goods.hasHeader()) { HSSFCellStyle headerStyle = getHeaderStyle(workbook); exportHeaders(sheet, _goods.getHeaders(), headerStyle); } // 3. to export data Object[] values; HSSFCellStyle dateStyle = getDateStyle(workbook); HSSFCellStyle doubleStyle = getDoubleStyle(workbook); int nRowNum = 1; Iterator iter = _goods.iterator(); while (iter.hasNext()) { values = _goods.unpackGoods(iter.next()); exportRow(sheet, values, nRowNum++, dateStyle, doubleStyle); } // 4. to output to stream workbook.write(_os); } catch (IOException ex) { throw new PureException(PureException.UNKNOWN, "export " + _goods.getName() + " to excel", ex); } }