List of usage examples for org.apache.poi.ss.usermodel Cell getCellStyle
CellStyle getCellStyle();
From source file:com.accenture.ts.dao.TesteCaseTSDAO.java
public boolean newTsSheet(String pathSheet, String nameSheet, List<TesteCaseTSBean> listTestCase) throws FileNotFoundException, IOException { boolean sucess = false; destinationSheet = new File(pathSheet); destinationSheet.mkdirs();// w ww .ja v a 2 s. com destinationSheet = new File(pathSheet + "\\" + nameSheet); sourceStheet = new File(sheetDefault); copySheet(sourceStheet, destinationSheet); FileInputStream fileSheet = new FileInputStream(destinationSheet); XSSFWorkbook workbook = new XSSFWorkbook(fileSheet); XSSFSheet sheetTS = workbook.getSheetAt(0); XSSFDataFormat format = workbook.createDataFormat(); XSSFCellStyle estilo = workbook.createCellStyle(); // String formatData = "aaaa-mm-dd\"T12:00:00-03:00\""; int linha = 1; Row row = sheetTS.getRow(linha); Cell descriptionPlan = row.getCell(0); Cell prj = row.getCell(1); Cell fase = row.getCell(2); Cell testPhase = row.getCell(3); Cell testScriptName = row.getCell(4); Cell testScriptDescription = row.getCell(5); Cell stepNo = row.getCell(6); Cell stepDescription = row.getCell(7); Cell expectedResults = row.getCell(8); Cell product = row.getCell(9); Cell dataPlanejada = row.getCell(10); if (listTestCase.size() > 0) { descriptionPlan.setCellValue(listTestCase.get(0).getTestPlan()); prj.setCellValue(listTestCase.get(0).getSTIPRJ()); fase.setCellValue(listTestCase.get(0).getFASE()); testPhase.setCellValue(listTestCase.get(0).getTestPhase()); } for (int i = 0; i < listTestCase.size(); i++) { // estilo.setDataFormat(format.getFormat(formatData)); // estilo.setFillBackgroundColor(HSSFColor.GREEN.index); row = sheetTS.getRow(linha); prj = row.getCell(1); fase = row.getCell(2); testPhase = row.getCell(3); testScriptName = row.getCell(4); testScriptDescription = row.getCell(5); stepNo = row.getCell(6); stepDescription = row.getCell(7); expectedResults = row.getCell(8); product = row.getCell(9); dataPlanejada = row.getCell(10); testScriptName.setCellValue(listTestCase.get(i).getTestScriptName()); testScriptDescription.setCellValue(listTestCase.get(i).getTestScriptDescription()); stepNo.setCellValue(listTestCase.get(i).getSTEP_NUMERO()); stepDescription.setCellValue(listTestCase.get(i).getStepDescription()); expectedResults.setCellValue(listTestCase.get(i).getExpectedResults()); product.setCellValue(listTestCase.get(i).getProduct()); estilo = (XSSFCellStyle) dataPlanejada.getCellStyle(); dataPlanejada.setCellValue(listTestCase.get(i).getDataPlanejada()); dataPlanejada.setCellStyle(estilo); linha = linha + 2; } FileOutputStream fileOut = new FileOutputStream(destinationSheet); workbook.write(fileOut); fileOut.close(); fileSheet.close(); sucess = true; return sucess; }
From source file:com.accenture.ts.dao.TesteCaseTSDAO.java
public boolean newTsSheet(String pathSheet, String nameSheet, TesteCaseTSBean testCase) throws FileNotFoundException, IOException { boolean sucess = false; destinationSheet = new File(pathSheet); destinationSheet.mkdirs();/*from w w w. j a v a 2 s.c o m*/ destinationSheet = new File(pathSheet + "\\" + nameSheet + ".xlsx"); sourceStheet = new File(sheetDefault); copySheet(sourceStheet, destinationSheet); FileInputStream fileSheet = new FileInputStream(destinationSheet); XSSFWorkbook workbook = new XSSFWorkbook(fileSheet); XSSFSheet sheetTS = workbook.getSheetAt(0); XSSFDataFormat format = workbook.createDataFormat(); XSSFCellStyle estilo = workbook.createCellStyle(); // String formatData = "aaaa-mm-dd\"T12:00:00-03:00\""; int linha = 1; Row row = sheetTS.getRow(linha); Cell descriptionPlan = row.getCell(0); Cell prj = row.getCell(1); Cell fase = row.getCell(2); Cell testPhase = row.getCell(3); Cell testScriptName = row.getCell(4); Cell testScriptDescription = row.getCell(5); Cell stepNo = row.getCell(6); Cell stepDescription = row.getCell(7); Cell expectedResults = row.getCell(8); Cell product = row.getCell(9); Cell dataPlanejada = row.getCell(10); Cell complexidade = row.getCell(11); Cell automatizado = row.getCell(12); descriptionPlan.setCellValue(testCase.getTestPlan()); prj.setCellValue(testCase.getSTIPRJ()); fase.setCellValue(testCase.getFASE()); testPhase.setCellValue(testCase.getTestPhase()); // estilo.setDataFormat(format.getFormat(formatData)); // estilo.setFillBackgroundColor(HSSFColor.GREEN.index); row = sheetTS.getRow(linha); prj = row.getCell(1); fase = row.getCell(2); testPhase = row.getCell(3); testScriptName = row.getCell(4); testScriptDescription = row.getCell(5); stepNo = row.getCell(6); stepDescription = row.getCell(7); expectedResults = row.getCell(8); product = row.getCell(9); dataPlanejada = row.getCell(10); complexidade = row.getCell(11); automatizado = row.getCell(12); testScriptName.setCellValue(testCase.getTestScriptName()); testScriptDescription.setCellValue(testCase.getTestScriptDescription()); stepNo.setCellValue(testCase.getSTEP_NUMERO()); stepDescription.setCellValue(testCase.getStepDescription()); expectedResults.setCellValue(testCase.getExpectedResults()); product.setCellValue(testCase.getProduct()); estilo = (XSSFCellStyle) dataPlanejada.getCellStyle(); dataPlanejada.setCellValue(FunctiosDates.getDateActual()); dataPlanejada.setCellStyle(estilo); complexidade.setCellValue(testCase.getComplexidade()); automatizado.setCellValue(testCase.isAutomatizado()); linha = linha + 2; FileOutputStream fileOut = new FileOutputStream(destinationSheet); workbook.write(fileOut); fileOut.close(); fileSheet.close(); sucess = true; return sucess; }
From source file:com.accenture.ts.dao.TesteCaseTSDAO.java
public boolean createSpreadsheetTS(String pathSheet, String nameSheet, TestPlanTSBean testPlan) throws Exception { boolean sucess = false; destinationSheet = new File(pathSheet); destinationSheet.mkdirs();// w w w . j a v a 2s .c o m String sheetTI = pathSheet + "\\" + nameSheet; nameSheet = nameSheet.replace("xlsx", "xlsm"); ; destinationSheet = new File(pathSheet + "\\" + nameSheet); sourceStheet = new File("C:\\FastPlan\\sheets\\TS_NEW.xlsm"); logger.info("Realizando cpia da planilha"); boolean existInList = false; List<TesteCaseTSBean> testCasesAutomatizados = new ArrayList<TesteCaseTSBean>(); //run macro String cmd = "C:\\FastPlan\\runMacro.vbs"; Runtime.getRuntime().exec("cmd /c" + cmd); Thread.sleep(2000); copySheet(sourceStheet, destinationSheet); logger.info("Planilha copiada"); FileInputStream fileSheet = new FileInputStream(destinationSheet); XSSFWorkbook workbook = new XSSFWorkbook(fileSheet); XSSFSheet sheetTS = workbook.getSheetAt(0); XSSFCellStyle estilo = workbook.createCellStyle(); // workbook.setSheetName(workbook.getSheetIndex(sheetTS), FunctiosDates.dateToString(FunctiosDates.getDateActual(), "yyyy-MM-dd-HH-mm-ss")); int linha = 2; Row row = sheetTS.getRow(linha); Cell descriptionPlan = row.getCell(0); Cell release = row.getCell(1); Cell prj = row.getCell(2); Cell fase = row.getCell(3); Cell testPhase = row.getCell(4); Cell testScriptName = row.getCell(5); Cell testScriptDescription = row.getCell(6); Cell stepNo = row.getCell(7); Cell stepDescription = row.getCell(8); Cell expectedResults = row.getCell(9); Cell product = row.getCell(10); Cell dataPlanejada = row.getCell(11); Cell qtdSteps = row.getCell(12); Cell complexidade = row.getCell(13); Cell automatizado = row.getCell(14); logger.info("Inserindo dados do plano"); descriptionPlan.setCellValue(testPlan.getName()); release.setCellValue(testPlan.getRelease()); for (int i = 0; i < testPlan.getTestCase().size(); i++) { row = sheetTS.getRow(linha); descriptionPlan = row.getCell(0); release = row.getCell(1); prj = row.getCell(2); fase = row.getCell(3); testPhase = row.getCell(4); testScriptName = row.getCell(5); testScriptDescription = row.getCell(6); stepNo = row.getCell(7); stepDescription = row.getCell(8); expectedResults = row.getCell(9); product = row.getCell(10); dataPlanejada = row.getCell(11); qtdSteps = row.getCell(12); complexidade = row.getCell(13); automatizado = row.getCell(14); logger.info("Inserindo dados dos TCs"); System.out.println("com.accenture.ts.dao.TesteCaseTSDAO.createSpreadsheetTS() - " + testPlan.getSti() + " - " + "row:" + linha); prj.setCellValue(testPlan.getSti()); fase.setCellValue(testPlan.getCrFase()); testPhase.setCellValue(testPlan.getTestPhase()); testScriptName.setCellValue(testPlan.getTestCase().get(i).getTestScriptName()); testScriptDescription.setCellValue(testPlan.getTestCase().get(i).getTestScriptDescription()); product.setCellValue(testPlan.getTestCase().get(i).getProduct()); estilo = (XSSFCellStyle) dataPlanejada.getCellStyle(); dataPlanejada.setCellValue(testPlan.getTestCase().get(i).getDataPlanejada()); dataPlanejada.setCellStyle(estilo); qtdSteps.setCellValue(testPlan.getTestCase().get(i).getListStep().size()); complexidade.setCellValue(testPlan.getTestCase().get(i).getComplexidade()); //set colors if (i % 2 == 0) { System.out.println("com.accenture.ts.dao.TesteCaseTSDAO.createSpreadsheetTS() - entrou"); // setColorCells(new Cell[]{descriptionPlan, release, prj, fase, testPhase, testScriptName, testScriptDescription, stepNo, // stepDescription, expectedResults, product, dataPlanejada, qtdSteps, complexidade}, workbook); XSSFCellStyle styleColor = (XSSFCellStyle) product.getCellStyle(); styleColor.setFillBackgroundColor(HSSFColor.LIGHT_GREEN.index); product.setCellStyle(styleColor); } for (int j = 0; j < testPlan.getTestCase().get(i).getListStep().size(); j++) { row = sheetTS.getRow(linha); stepNo = row.getCell(7); stepDescription = row.getCell(8); expectedResults = row.getCell(9); // // stepNo.setCellValue(testPlan.getTestCase().get(i).getListStep().get(j).getNomeStep()); logger.info("Inserindo dados dos Steps"); stepNo.setCellValue(j + 1); stepDescription.setCellValue(testPlan.getTestCase().get(i).getListStep().get(j).getDescStep()); expectedResults.setCellValue(testPlan.getTestCase().get(i).getListStep().get(j).getResultadoStep()); linha = linha + 1; row = sheetTS.getRow(linha); stepNo = row.getCell(7); stepDescription = row.getCell(8); expectedResults = row.getCell(9); } linha = linha + 1; row = sheetTS.getRow(linha); descriptionPlan = row.getCell(0); release = row.getCell(1); prj = row.getCell(2); fase = row.getCell(3); testPhase = row.getCell(4); testScriptName = row.getCell(5); testScriptDescription = row.getCell(6); stepNo = row.getCell(7); stepDescription = row.getCell(8); expectedResults = row.getCell(9); product = row.getCell(10); dataPlanejada = row.getCell(11); qtdSteps = row.getCell(12); complexidade = row.getCell(13); automatizado = row.getCell(14); logger.info("Dados inseridos na planilha"); if (testPlan.getTestCase().get(i).isAutomatizado()) { for (int j = 0; j < testCasesAutomatizados.size(); j++) { if (testPlan.getTestCase().get(i).equals(testCasesAutomatizados.get(j).getTestScriptName())) { existInList = true; } } if (!existInList) { testCasesAutomatizados.add(testPlan.getTestCase().get(i)); existInList = true; } } } ExtraiPlanilha extraiPlanilha = new ExtraiPlanilha(); extraiPlanilha.exportTStoTI(testCasesAutomatizados, sheetTI); logger.info("Preparando para salvar planilha"); FileOutputStream fileOut = new FileOutputStream(destinationSheet); logger.info("Fim mtodo - new FileOutputStream(destinationSheet) "); logger.info("Tentando gravar na planilha."); workbook.write(fileOut); logger.info("Fim mtodo - workbook.write(fileOut)"); fileOut.close(); fileSheet.close(); sucess = true; logger.info("Planilha gerada."); return sucess; }
From source file:com.accenture.ts.dao.TesteCaseTSDAO.java
public boolean newTsSheet(String pathSheet, String nameSheet, TestPlanTSBean testPlan) throws Exception { boolean sucess = false; destinationSheet = new File(pathSheet); destinationSheet.mkdirs();/* w ww .java2 s . c om*/ destinationSheet = new File(pathSheet + "\\" + nameSheet); sourceStheet = new File(sheetDefault); copySheet(sourceStheet, destinationSheet); FileInputStream fileSheet = new FileInputStream(destinationSheet); XSSFWorkbook workbook = new XSSFWorkbook(fileSheet); XSSFSheet sheetTS = workbook.getSheetAt(0); XSSFDataFormat format = workbook.createDataFormat(); XSSFCellStyle estilo = workbook.createCellStyle(); // String formatData = "aaaa-mm-dd\"T12:00:00-03:00\""; int linha = 1; Row row = sheetTS.getRow(linha); Cell descriptionPlan = row.getCell(0); Cell prj = row.getCell(1); Cell fase = row.getCell(2); Cell testPhase = row.getCell(3); Cell testScriptName = row.getCell(4); Cell testScriptDescription = row.getCell(5); Cell stepNo = row.getCell(6); Cell stepDescription = row.getCell(7); Cell expectedResults = row.getCell(8); Cell product = row.getCell(9); Cell dataPlanejada = row.getCell(10); Cell complexidade = row.getCell(11); descriptionPlan.setCellValue(testPlan.getName()); prj.setCellValue(testPlan.getSti()); fase.setCellValue(testPlan.getCrFase()); testPhase.setCellValue(testPlan.getTestPhase()); // estilo.setDataFormat(format.getFormat(formatData)); // estilo.setFillBackgroundColor(HSSFColor.GREEN.index); for (int i = 0; i < testPlan.getTestCase().size(); i++) { row = sheetTS.getRow(linha); prj = row.getCell(1); fase = row.getCell(2); testPhase = row.getCell(3); testScriptName = row.getCell(4); testScriptDescription = row.getCell(5); stepNo = row.getCell(6); stepDescription = row.getCell(7); expectedResults = row.getCell(8); product = row.getCell(9); dataPlanejada = row.getCell(10); complexidade = row.getCell(11); testScriptName.setCellValue(testPlan.getTestCase().get(i).getTestScriptName()); testScriptDescription.setCellValue(testPlan.getTestCase().get(i).getTestScriptDescription()); stepNo.setCellValue(testPlan.getTestCase().get(i).getSTEP_NUMERO()); stepDescription.setCellValue(testPlan.getTestCase().get(i).getStepDescription()); expectedResults.setCellValue(testPlan.getTestCase().get(i).getExpectedResults()); product.setCellValue(testPlan.getTestCase().get(i).getProduct()); estilo = (XSSFCellStyle) dataPlanejada.getCellStyle(); dataPlanejada.setCellValue(testPlan.getTestCase().get(i).getDataPlanejada()); dataPlanejada.setCellStyle(estilo); complexidade.setCellValue(testPlan.getTestCase().get(i).getComplexidade()); linha = linha + 2; row = sheetTS.getRow(linha); prj = row.getCell(1); fase = row.getCell(2); testPhase = row.getCell(3); testScriptName = row.getCell(4); testScriptDescription = row.getCell(5); stepNo = row.getCell(6); stepDescription = row.getCell(7); expectedResults = row.getCell(8); product = row.getCell(9); dataPlanejada = row.getCell(10); complexidade = row.getCell(11); } FileOutputStream fileOut = new FileOutputStream(destinationSheet); workbook.write(fileOut); fileOut.close(); fileSheet.close(); sucess = true; return sucess; }
From source file:com.accenture.ts.dao.TesteCaseTSDAO.java
public boolean updateTsSheet(String pathSheet, String nameSheet, TesteCaseTSBean testCase) throws FileNotFoundException, IOException { boolean sucess = false; destinationSheet = new File(pathSheet); destinationSheet.mkdirs();//w w w .ja v a 2 s.c o m destinationSheet = new File(pathSheet + "\\" + nameSheet); sourceStheet = new File(sheetDefault); copySheet(sourceStheet, destinationSheet); FileInputStream fileSheet = new FileInputStream(destinationSheet); XSSFWorkbook workbook = new XSSFWorkbook(fileSheet); XSSFSheet sheetTS = workbook.getSheetAt(0); XSSFDataFormat format = workbook.createDataFormat(); XSSFCellStyle estilo = workbook.createCellStyle(); // String formatData = "aaaa-mm-dd\"T12:00:00-03:00\""; int linha = 1; Row row = sheetTS.getRow(linha); Cell descriptionPlan = row.getCell(0); Cell prj = row.getCell(1); Cell fase = row.getCell(2); Cell testPhase = row.getCell(3); Cell testScriptName = row.getCell(4); Cell testScriptDescription = row.getCell(5); Cell stepNo = row.getCell(6); Cell stepDescription = row.getCell(7); Cell expectedResults = row.getCell(8); Cell product = row.getCell(9); Cell dataPlanejada = row.getCell(10); Cell complexidade = row.getCell(11); Cell automatizado = row.getCell(12); descriptionPlan.setCellValue(testCase.getTestPlan()); prj.setCellValue(testCase.getSTIPRJ()); fase.setCellValue(testCase.getFASE()); testPhase.setCellValue(testCase.getTestPhase()); // estilo.setDataFormat(format.getFormat(formatData)); // estilo.setFillBackgroundColor(HSSFColor.GREEN.index); row = sheetTS.getRow(linha); prj = row.getCell(1); fase = row.getCell(2); testPhase = row.getCell(3); testScriptName = row.getCell(4); testScriptDescription = row.getCell(5); stepNo = row.getCell(6); stepDescription = row.getCell(7); expectedResults = row.getCell(8); product = row.getCell(9); dataPlanejada = row.getCell(10); complexidade = row.getCell(11); automatizado = row.getCell(12); testScriptName.setCellValue(testCase.getTestScriptName()); testScriptDescription.setCellValue(testCase.getTestScriptDescription()); stepNo.setCellValue(testCase.getSTEP_NUMERO()); stepDescription.setCellValue(testCase.getStepDescription()); expectedResults.setCellValue(testCase.getExpectedResults()); product.setCellValue(testCase.getProduct()); estilo = (XSSFCellStyle) dataPlanejada.getCellStyle(); dataPlanejada.setCellValue(testCase.getDataPlanejada()); dataPlanejada.setCellStyle(estilo); complexidade.setCellValue(testCase.getComplexidade()); automatizado.setCellValue(testCase.isAutomatizado()); linha = linha + 2; FileOutputStream fileOut = new FileOutputStream(destinationSheet); workbook.write(fileOut); fileOut.close(); fileSheet.close(); sucess = true; return sucess; }
From source file:com.actelion.research.spiritapp.report.AbstractReport.java
License:Open Source License
public void populateReport(Study study) throws Exception { assert study != null; this.study = study; initWorkbook();/*from ww w. ja va 2 s. c om*/ //Create the workbook populateWorkBook(); //Post processing //Add Table borders (between different styles of cells) for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i); for (int r = 4; r <= sheet.getLastRowNum(); r++) { Row row = sheet.getRow(r); if (row == null) continue; Row rowUp = sheet.getRow(r - 1); Row rowDown = sheet.getRow(r + 1); for (int c = 0; c <= row.getLastCellNum(); c++) { Cell cell = row.getCell(c); Cell cellLeft = c == 0 ? null : row.getCell(c - 1); boolean borderLeftAbove = cellLeft != null && cellLeft.getCellStyle().getBorderTop() == 1; boolean borderLeftUnder = cellLeft != null && cellLeft.getCellStyle().getBorderBottom() == 1; if ((cell != null && cell.getCellStyle().getBorderLeft() + cell.getCellStyle().getBorderRight() > 0) || (cell == null && c + 1 <= row.getLastCellNum() && row.getCell(c + 1) != null)) { if (borderLeftAbove) drawLineAbove(sheet, r, c, c, (short) 1); if (borderLeftUnder) drawLineUnder(sheet, r, c, c, (short) 1); } if (cell != null) { Font font = wb.getFontAt(cell.getCellStyle().getFontIndex()); if (font.getFontHeightInPoints() >= 12) continue; Cell cellUp = rowUp != null && c < rowUp.getLastCellNum() ? rowUp.getCell(c) : null; Cell cellDown = rowDown != null && c < rowDown.getLastCellNum() ? rowDown.getCell(c) : null; if (cellUp == null /*|| (cell.getCellType()!=0 && cellUp.getCellType()!=0 && cellUp.getCellType()!=cell.getCellType())*/ ) { //Border above drawLineAbove(sheet, r, c, c, (short) 1); } if (cellDown == null /*|| (cell.getCellType()!=0 && cellDown.getCellType()!=0 && cellDown.getCellType()!=cell.getCellType())*/) { //Border under drawLineUnder(sheet, r, c, c, (short) 1); } } } } } }
From source file:com.actelion.research.spiritapp.report.AbstractReport.java
License:Open Source License
protected Cell set(Sheet sheet, int row, int col, Object text, Style style, int rowspan, int colspan) { Row r = sheet.getRow(row);/*from w w w. ja v a2s. c o m*/ if (r == null) r = sheet.createRow(row); Cell c = r.getCell(col); if (c == null) c = r.createCell(col); c.setCellStyle(styles.get(style)); if (text == null) { if (c.getCellStyle().getDataFormatString().startsWith("0")) { c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellValue(""); } else { c.setCellType(Cell.CELL_TYPE_STRING); c.setCellValue(""); } } else if (text instanceof String) { try { c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellValue(Integer.parseInt((String) text)); } catch (Exception e) { try { c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellValue(Double.parseDouble((String) text)); } catch (Exception e2) { c.setCellType(Cell.CELL_TYPE_STRING); c.setCellValue((String) text); } } } else if (text instanceof Double) { c.setCellValue((Double) text); c.setCellType(Cell.CELL_TYPE_NUMERIC); } else if (text instanceof Integer) { c.setCellValue((Integer) text); c.setCellType(Cell.CELL_TYPE_NUMERIC); } else if (text instanceof Date) { c.setCellValue((Date) text); } if (rowspan > 1 || colspan > 1) { sheet.addMergedRegion(new CellRangeAddress(row, row + rowspan - 1, col, col + colspan - 1)); for (int i = 0; i < rowspan; i++) { for (int j = 0; j < colspan; j++) { if (i > 0 || j > 0) set(sheet, row + i, col + j, "", style); } } } return c; }
From source file:com.actelion.research.spiritapp.report.AbstractReport.java
License:Open Source License
protected void drawLineUnder(Sheet sheet, int row, int colMin, int colMax, short thickness) { Row r = sheet.getRow(row);/*from w w w . j av a 2s . c om*/ if (r == null) r = sheet.createRow(row); for (int col = colMin; col <= colMax; col++) { Cell c = r.getCell(col); if (c == null) c = r.createCell(col); CellStyle style = styleWithBordersUnder.get((c.getCellStyle().getIndex() << 4) + thickness); if (style == null) { style = sheet.getWorkbook().createCellStyle(); style.cloneStyleFrom(c.getCellStyle()); style.setBorderBottom(thickness); styleWithBordersUnder.put((c.getCellStyle().getIndex() << 4) + thickness, style); } c.setCellStyle(style); } }
From source file:com.actelion.research.spiritapp.report.AbstractReport.java
License:Open Source License
protected void drawLineAbove(Sheet sheet, int row, int colMin, int colMax, short thickness) { Row r = sheet.getRow(row);// w w w . j a v a 2 s .com if (r == null) r = sheet.createRow(row); for (int col = colMin; col <= colMax; col++) { Cell c = r.getCell(col); if (c == null) c = r.createCell(col); CellStyle style = styleWithBordersAbove.get(c.getCellStyle().getIndex() << 4 + thickness); if (style == null) { style = sheet.getWorkbook().createCellStyle(); style.cloneStyleFrom(c.getCellStyle()); style.setBorderTop(thickness); styleWithBordersAbove.put(c.getCellStyle().getIndex() << 4 + thickness, style); } c.setCellStyle(style); } }
From source file:com.actelion.research.spiritapp.ui.util.PDFUtils.java
License:Open Source License
public static void convertHSSF2Pdf(Workbook wb, String header, File reportFile) throws Exception { assert wb != null; assert reportFile != null; //Precompute formula FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); for (int i = 0; i < wb.getNumberOfSheets(); i++) { Sheet sheet = wb.getSheetAt(i);//from w w w. jav a2s . c o m for (Row r : sheet) { for (Cell c : r) { if (c.getCellType() == HSSFCell.CELL_TYPE_FORMULA) { try { evaluator.evaluateFormulaCell(c); } catch (Exception e) { System.err.println(e); } } } } } File tmp = File.createTempFile("tmp_", ".xlsx"); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(tmp))) { wb.write(out); } //Find page orientation int maxColumnsGlobal = 0; for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) { Sheet sheet = wb.getSheetAt(sheetNo); for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) { Row row = rowIterator.next(); maxColumnsGlobal = Math.max(maxColumnsGlobal, row.getLastCellNum()); } } Rectangle pageSize = maxColumnsGlobal < 10 ? PageSize.A4 : PageSize.A4.rotate(); Document pdfDocument = new Document(pageSize, 10f, 10f, 20f, 20f); PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(reportFile)); addHeader(writer, header); pdfDocument.open(); //we have two columns in the Excel sheet, so we create a PDF table with two columns //Note: There are ways to make this dynamic in nature, if you want to. //Loop through sheets for (int sheetNo = 0; sheetNo < wb.getNumberOfSheets(); sheetNo++) { Sheet sheet = wb.getSheetAt(sheetNo); //Loop through rows, to find number of columns int minColumns = 1000; int maxColumns = 0; for (Iterator<Row> rowIterator = sheet.iterator(); rowIterator.hasNext();) { Row row = rowIterator.next(); if (row.getFirstCellNum() >= 0) minColumns = Math.min(minColumns, row.getFirstCellNum()); if (row.getLastCellNum() >= 0) maxColumns = Math.max(maxColumns, row.getLastCellNum()); } if (maxColumns == 0) continue; //Loop through first rows, to find relative width float[] widths = new float[maxColumns]; int totalWidth = 0; for (int c = 0; c < maxColumns; c++) { int w = sheet.getColumnWidth(c); widths[c] = w; totalWidth += w; } for (int c = 0; c < maxColumns; c++) { widths[c] /= totalWidth; } //Create new page and a new chapter with the sheet's name if (sheetNo > 0) pdfDocument.newPage(); Chapter pdfSheet = new Chapter(sheet.getSheetName(), sheetNo + 1); PdfPTable pdfTable = null; PdfPCell pdfCell = null; boolean inTable = false; //Loop through cells, to create the content // boolean leftBorder = true; // boolean[] topBorder = new boolean[maxColumns+1]; for (int r = 0; r <= sheet.getLastRowNum(); r++) { Row row = sheet.getRow(r); //Check if we exited a table (empty line) if (row == null) { if (pdfTable != null) { addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable); pdfTable = null; } inTable = false; continue; } //Check if we start a table (>MIN_COL_IN_TABLE columns) if (row.getLastCellNum() >= MIN_COL_IN_TABLE) { inTable = true; } if (!inTable) { //Process the data outside table, just add the text boolean hasData = false; Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); if (cell.getCellType() == Cell.CELL_TYPE_BLANK) continue; Chunk chunk = getChunk(wb, cell); pdfSheet.add(chunk); pdfSheet.add(new Chunk(" ")); hasData = true; } if (hasData) pdfSheet.add(Chunk.NEWLINE); } else { //Process the data in table if (pdfTable == null) { //Create table pdfTable = new PdfPTable(maxColumns); pdfTable.setWidths(widths); // topBorder = new boolean[maxColumns+1]; } int cellNumber = minColumns; // leftBorder = false; Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); for (; cellNumber < cell.getColumnIndex(); cellNumber++) { pdfCell = new PdfPCell(); pdfCell.setBorder(0); pdfTable.addCell(pdfCell); } Chunk phrase = getChunk(wb, cell); pdfCell = new PdfPCell(new Phrase(phrase)); pdfCell.setFixedHeight(row.getHeightInPoints() - 3); pdfCell.setNoWrap(!cell.getCellStyle().getWrapText()); pdfCell.setPaddingLeft(1); pdfCell.setHorizontalAlignment( cell.getCellStyle().getAlignment() == CellStyle.ALIGN_CENTER ? PdfPCell.ALIGN_CENTER : cell.getCellStyle().getAlignment() == CellStyle.ALIGN_RIGHT ? PdfPCell.ALIGN_RIGHT : PdfPCell.ALIGN_LEFT); pdfCell.setUseBorderPadding(false); pdfCell.setUseVariableBorders(false); pdfCell.setBorderWidthRight(cell.getCellStyle().getBorderRight() == 0 ? 0 : .5f); pdfCell.setBorderWidthBottom(cell.getCellStyle().getBorderBottom() == 0 ? 0 : cell.getCellStyle().getBorderBottom() > 1 ? 1 : .5f); pdfCell.setBorderWidthLeft(cell.getCellStyle().getBorderLeft() == 0 ? 0 : cell.getCellStyle().getBorderLeft() > 1 ? 1 : .5f); pdfCell.setBorderWidthTop(cell.getCellStyle().getBorderTop() == 0 ? 0 : cell.getCellStyle().getBorderTop() > 1 ? 1 : .5f); String color = cell.getCellStyle().getFillForegroundColorColor() == null ? null : ((XSSFColor) cell.getCellStyle().getFillForegroundColorColor()).getARGBHex(); if (color != null) pdfCell.setBackgroundColor(new Color(Integer.decode("0x" + color.substring(2)))); pdfTable.addCell(pdfCell); cellNumber++; } for (; cellNumber < maxColumns; cellNumber++) { pdfCell = new PdfPCell(); pdfCell.setBorder(0); pdfTable.addCell(pdfCell); } } //Custom code to add all images on the first sheet (works for reporting) if (sheetNo == 0 && row.getRowNum() == 0) { for (PictureData pd : wb.getAllPictures()) { try { Image pdfImg = Image.getInstance(pd.getData()); pdfImg.scaleToFit( pageSize.getWidth() * .8f - pageSize.getBorderWidthLeft() - pageSize.getBorderWidthRight(), pageSize.getHeight() * .8f - pageSize.getBorderWidthTop() - pageSize.getBorderWidthBottom()); pdfSheet.add(pdfImg); } catch (Exception e) { e.printStackTrace(); } } } } if (pdfTable != null) { addTable(pdfDocument, pdfSheet, totalWidth, widths, pdfTable); } pdfDocument.add(pdfSheet); } pdfDocument.close(); }