List of usage examples for org.apache.poi.hssf.usermodel HSSFSheet getRow
@Override public HSSFRow getRow(int rowIndex)
From source file:com.nkapps.billing.services.BankStatementPrintServiceImpl.java
@Override public HSSFWorkbook printClaimRegister(List<PrintClaimRegisterPojo> listPojo) throws Exception { HSSFWorkbook workbook = null;/*from ww w . jav a 2s . c om*/ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( servletContext.getRealPath("templates") + File.separator + "print_claim_register.xls")); workbook = new HSSFWorkbook(fs); HSSFSheet sheet = workbook.getSheetAt(0); // title //HSSFRow row = sheet.getRow(1); //HSSFCell cell = row.getCell(1); //String title; //cell.setCellValue(title); // HSSFRow row; final int CELL_START = 1; final int ROW_START = sheet.getLastRowNum() - 1; final int CELL_END = sheet.getRow(ROW_START).getLastCellNum(); int rowCurrent = ROW_START, rowTotal = sheet.getLastRowNum(), cellCurrent; // HSSFCellStyle[] style = new HSSFCellStyle[CELL_END]; short styleHeight = sheet.getRow(rowCurrent).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { style[i] = sheet.getRow(rowCurrent).getCell(i).getCellStyle(); } // total style HSSFCellStyle[] totalStyle = new HSSFCellStyle[CELL_END]; short totalStyleHeight = sheet.getRow(rowTotal).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { totalStyle[i] = sheet.getRow(rowTotal).getCell(i).getCellStyle(); } SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); int numQuantity = 0; for (PrintClaimRegisterPojo pcrp : listPojo) { numQuantity++; row = sheet.createRow(rowCurrent++); row.setHeight(styleHeight); cellCurrent = CELL_START; createCell(row, cellCurrent++, style, pcrp.getInvoiceNum()); createCell(row, cellCurrent++, style, pcrp.getPaymentNum()); createCell(row, cellCurrent++, style, dateFormat.format(pcrp.getPaymentDate())); createCell(row, cellCurrent++, style, pcrp.getTin()); createCell(row, cellCurrent++, style, pcrp.getName()); createCell(row, cellCurrent++, style, pcrp.getPaymentSum()); } List<Integer> totalCellIndexes = new ArrayList<>(); totalCellIndexes.add(CELL_START + 5); // for payment sum columns createTotalCells(totalCellIndexes, sheet, totalStyle, totalStyleHeight, ROW_START, CELL_START, rowCurrent); return workbook; }
From source file:com.nkapps.billing.services.BankStatementPrintServiceImpl.java
@Override public HSSFWorkbook printPaymentManual(BankStatement bs) throws Exception { HSSFWorkbook workbook = null;/*from ww w .j a v a2s . co m*/ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream( servletContext.getRealPath("templates") + File.separator + "payment_manual.xls")); workbook = new HSSFWorkbook(fs); HSSFSheet sheet = workbook.getSheetAt(0); HSSFRow row = sheet.getRow(0); row.getCell(1).setCellValue(bs.getId()); final int CELL_START = 1; final int ROW_START = sheet.getLastRowNum(); final int CELL_END = sheet.getRow(ROW_START).getLastCellNum(); int rowCurrent = ROW_START, cellCurrent; // HSSFCellStyle[] style = new HSSFCellStyle[CELL_END]; short styleHeight = sheet.getRow(rowCurrent).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { style[i] = sheet.getRow(rowCurrent).getCell(i).getCellStyle(); } SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); row = sheet.createRow(3); createCell(row, 1, style, ""); createCell(row, 2, style, bs.getMfo()); createCell(row, 3, style, bs.getChet()); createCell(row, 4, style, bs.getPaymentNum()); createCell(row, 5, style, dateFormat.format(bs.getPaymentDate())); createCell(row, 6, style, ""); createCell(row, 7, style, bs.getTin()); return workbook; }
From source file:com.nkapps.billing.services.ReportPrintServiceImpl.java
@Override public HSSFWorkbook printClick(List<ReportClickListPojo> listPojos) throws Exception { HSSFWorkbook workbook = null;/*w w w .j a v a 2 s. c o m*/ POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream(servletContext.getRealPath("templates") + File.separator + "report_click.xls")); workbook = new HSSFWorkbook(fs); HSSFSheet sheet = workbook.getSheetAt(0); // title //HSSFRow row = sheet.getRow(1); //HSSFCell cell = row.getCell(1); //String title; //cell.setCellValue(title); // HSSFRow row; final int CELL_START = 1; final int ROW_START = sheet.getLastRowNum() - 1; final int CELL_END = sheet.getRow(ROW_START).getLastCellNum(); int rowCurrent = ROW_START, rowTotal = sheet.getLastRowNum(), cellCurrent; // HSSFCellStyle[] style = new HSSFCellStyle[CELL_END]; short styleHeight = sheet.getRow(rowCurrent).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { style[i] = sheet.getRow(rowCurrent).getCell(i).getCellStyle(); } // total style HSSFCellStyle[] totalStyle = new HSSFCellStyle[CELL_END]; short totalStyleHeight = sheet.getRow(rowTotal).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { totalStyle[i] = sheet.getRow(rowTotal).getCell(i).getCellStyle(); } SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); int numQuantity = 0; for (ReportClickListPojo rclp : listPojos) { numQuantity++; row = sheet.createRow(rowCurrent++); row.setHeight(styleHeight); cellCurrent = CELL_START; createCell(row, cellCurrent++, style, numQuantity); createCell(row, cellCurrent++, style, dateFormat.format(rclp.getOperationDate())); createCell(row, cellCurrent++, style, dateFormat.format(rclp.getBsPaymentDate())); createCell(row, cellCurrent++, style, rclp.getBsPaymentNum()); createCell(row, cellCurrent++, style, rclp.getBsPaymentSum()); createCell(row, cellCurrent++, style, rclp.getClickPaymentSum()); createCell(row, cellCurrent++, style, rclp.getDiffSum()); } List<Integer> totalCellIndexes = new ArrayList<>(); totalCellIndexes.add(CELL_START + 4); // for totalCellIndexes.add(CELL_START + 5); // for totalCellIndexes.add(CELL_START + 6); // for createTotalCells(totalCellIndexes, sheet, totalStyle, totalStyleHeight, ROW_START, CELL_START, rowCurrent); return workbook; }
From source file:com.nkapps.billing.services.ReportPrintServiceImpl.java
@Override public HSSFWorkbook printSmst(List<ReportSmstListPojo> listPojos) throws Exception { HSSFWorkbook workbook = null;/*ww w . java2s . c om*/ POIFSFileSystem fs = new POIFSFileSystem( new FileInputStream(servletContext.getRealPath("templates") + File.separator + "report_smst.xls")); workbook = new HSSFWorkbook(fs); HSSFSheet sheet = workbook.getSheetAt(0); // title //HSSFRow row = sheet.getRow(1); //HSSFCell cell = row.getCell(1); //String title; //cell.setCellValue(title); // HSSFRow row; final int CELL_START = 1; final int ROW_START = sheet.getLastRowNum() - 1; final int CELL_END = sheet.getRow(ROW_START).getLastCellNum(); int rowCurrent = ROW_START, rowTotal = sheet.getLastRowNum(), cellCurrent; // HSSFCellStyle[] style = new HSSFCellStyle[CELL_END]; short styleHeight = sheet.getRow(rowCurrent).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { style[i] = sheet.getRow(rowCurrent).getCell(i).getCellStyle(); } // total style HSSFCellStyle[] totalStyle = new HSSFCellStyle[CELL_END]; short totalStyleHeight = sheet.getRow(rowTotal).getHeight(); for (int i = CELL_START; i < CELL_END; i++) { totalStyle[i] = sheet.getRow(rowTotal).getCell(i).getCellStyle(); } SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); int numQuantity = 0; for (ReportSmstListPojo rslp : listPojos) { numQuantity++; row = sheet.createRow(rowCurrent++); row.setHeight(styleHeight); cellCurrent = CELL_START; createCell(row, cellCurrent++, style, numQuantity); createCell(row, cellCurrent++, style, dateFormat.format(rslp.getOperationDate())); createCell(row, cellCurrent++, style, rslp.getBsPaymentSum()); createCell(row, cellCurrent++, style, rslp.getSmstPaymentSum()); createCell(row, cellCurrent++, style, rslp.getDiffSum()); } List<Integer> totalCellIndexes = new ArrayList<>(); totalCellIndexes.add(CELL_START + 2); // for totalCellIndexes.add(CELL_START + 3); // for totalCellIndexes.add(CELL_START + 4); // for createTotalCells(totalCellIndexes, sheet, totalStyle, totalStyleHeight, ROW_START, CELL_START, rowCurrent); return workbook; }
From source file:com.pdf.GetPdf.java
public static void addXls(Document document, String url, String type) throws IOException, DocumentException { Iterator<Row> rowIterator; int colNo;//from w w w . java 2 s.com if (type.equals("xls")) { HSSFWorkbook excelWorkbook = new HSSFWorkbook(new URL(url).openStream()); HSSFSheet my_worksheet = excelWorkbook.getSheetAt(0); rowIterator = my_worksheet.iterator(); colNo = my_worksheet.getRow(0).getLastCellNum(); } else { XSSFWorkbook excelWorkbook1 = new XSSFWorkbook(new URL(url).openStream()); XSSFSheet my_worksheet = excelWorkbook1.getSheetAt(0); rowIterator = my_worksheet.iterator(); colNo = my_worksheet.getRow(0).getLastCellNum(); } PdfPTable my_table = new PdfPTable(colNo); PdfPCell table_cell = null; while (rowIterator.hasNext()) { Row row = rowIterator.next(); //Read Rows from Excel document Iterator<Cell> cellIterator = row.cellIterator();//Read every column for every row that is READ while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); //Fetch CELL if (cell.getCellType() == (Cell.CELL_TYPE_NUMERIC)) { table_cell = new PdfPCell(new Phrase(new Double(cell.getNumericCellValue()).toString())); System.out.println(cell.getNumericCellValue()); my_table.addCell(table_cell); } else if (cell.getCellType() == (Cell.CELL_TYPE_STRING)) { table_cell = new PdfPCell(new Phrase(cell.getStringCellValue())); System.out.println(cell.getStringCellValue()); my_table.addCell(table_cell); } else if (cell.getCellType() == (Cell.CELL_TYPE_FORMULA)) { table_cell = new PdfPCell(new Phrase(cell.getCellFormula())); my_table.addCell(table_cell); } else if (cell.getCellType() == (Cell.CELL_TYPE_BLANK)) { table_cell = new PdfPCell(new Phrase("")); my_table.addCell(table_cell); } else { table_cell = new PdfPCell(new Phrase("")); my_table.addCell(table_cell); } } } document.add(my_table); }
From source file:com.pe.nisira.movil.view.action.RegistroPaleta.java
public void postProcessXLS(Object document) { HSSFWorkbook wb = (HSSFWorkbook) document; HSSFSheet sheet = wb.getSheetAt(0); HSSFRow header = sheet.getRow(0); HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setFillForegroundColor(HSSFColor.GREEN.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) { header.getCell(i).setCellStyle(cellStyle); }/* w w w . j a v a 2 s . c o m*/ }
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 ava 2 s . c om*/ 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 void writeTestCaseToXLS(String testSuiteName, String[] cellValue, String status, int numCol, int cellno, CellStyle[] tryStyle, StringBuilder sb) throws PhrescoException { Iterator<Row> rowIterator; try {/*from ww w . jav a 2s.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.preparatic.csvreaders.FicheroExcel.java
License:Apache License
private void recorrerExcel() { this.listaPreguntas = new ArrayList<PreguntaTest>(); /*/*from w ww . j a va 2 s. com*/ * Obtenemos la primera pestaa que se quiera procesar indicando el * ndice. Una vez obtenida la hoja excel con las filas que se quieren * leer obtenemos el iterator que nos permite recorrer cada una de las * filas que contiene. */ HSSFSheet sheet = this.fichero.getSheetAt(0); ArrayList<String> celdasPregunta = null; // Recorremos todas las filas para obtener el contenido de cada celda // int NumberOfRows = 8493; // sheet.getPhysicalNumberOfRows() devuelve // 1.000 filas adicionales. Para evitar esto, ponemos el n // directamente. int NumberOfRows = 11; for (int rowIndex = 0; rowIndex < NumberOfRows; rowIndex++) { Row fila = sheet.getRow(rowIndex); celdasPregunta = new ArrayList<String>(); for (int columna = 0; columna < 20; columna++) { // If the cell is missing from the file, generate a blank one // (Works by specifying a MissingCellPolicy) Cell celda = fila.getCell(columna, Row.CREATE_NULL_AS_BLANK); celda.setCellType(Cell.CELL_TYPE_STRING); celdasPregunta.add(celda.getStringCellValue()); } this.listaPreguntas.add(new PreguntaTest(celdasPregunta)); } try { fichero.close(); // cerramos el libro excel } catch (IOException e) { e.printStackTrace(); } }
From source file:com.primovision.lutransport.core.util.TollCompanyTagUploadUtil.java
public static ByteArrayOutputStream createTollUploadErrorResponse(InputStream is, List<String> errors) throws IOException { POIFSFileSystem fs = new POIFSFileSystem(is); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFFont font = wb.createFont();/*www . j a v a 2 s . co m*/ font.setColor(Font.COLOR_RED); font.setBoldweight(Font.BOLDWEIGHT_BOLD); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setFont(font); HSSFSheet sheet = wb.getSheetAt(0); Row row = sheet.getRow(0); int lastCell = row.getLastCellNum(); Cell cell = createExcelCell(sheet, row, lastCell, 256 * 100); cell.setCellStyle(cellStyle); cell.setCellValue("ERRORS"); for (String anError : errors) { String lineNoStr = StringUtils.substringBefore(anError, ":"); lineNoStr = StringUtils.substringAfter(lineNoStr, "Line "); Integer lineNo = new Integer(lineNoStr) - 1; row = sheet.getRow(lineNo); cell = createExcelCell(sheet, row, lastCell, 256 * 100); cell.setCellStyle(cellStyle); cell.setCellValue(anError); } return createOutputStream(wb); }