List of usage examples for org.apache.poi.xssf.usermodel XSSFSheet autoSizeColumn
@Override public void autoSizeColumn(int column)
From source file:br.com.jinsync.controller.ExportExcelBook.java
License:Open Source License
@Override protected Object doInBackground() throws Exception { final String dir = System.getProperty("user.dir") + "\\layouts"; File arqProp = new File(dir); if (!arqProp.exists()) { arqProp.mkdirs();/* w ww . java 2 s . c om*/ } String nameFile = this.nameFile; int lin = 0; int col = 0; int pos = 0; int posFim = 0; pos = nameFile.lastIndexOf("("); if (pos > 0) { posFim = nameFile.lastIndexOf(")"); nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_layout" + ".xlsx"; } else { pos = nameFile.lastIndexOf("\\"); if (pos > 0) { posFim = nameFile.lastIndexOf("."); if (posFim > 0) { nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_layout" + ".xlsx"; } else { nameFile = dir + nameFile.substring(pos) + "_layout" + ".xlsx"; } } } FileOutputStream out; try { out = new FileOutputStream(nameFile); TableModel model = tableName.getModel(); XSSFWorkbook wb = new XSSFWorkbook(); // Criando area de trabalho // para o excel XSSFSheet s = wb.createSheet(); // criando uma nova sheet XSSFFont f = wb.createFont(); XSSFFont f2 = wb.createFont(); XSSFCellStyle cs = wb.createCellStyle(); XSSFCellStyle cs2 = wb.createCellStyle(); XSSFCellStyle cs3 = wb.createCellStyle(); XSSFCellStyle cs4 = wb.createCellStyle(); f.setFontHeightInPoints((short) 8); f2.setFontHeightInPoints((short) 8); f.setBoldweight(Font.BOLDWEIGHT_BOLD); f2.setBoldweight(Font.BOLDWEIGHT_NORMAL); f.setFontName("Courier New"); f2.setFontName("Courier New"); XSSFRow r = null; // Criando uma referencia para Linha XSSFCell c = null; // Referencia para Celula cs.setFont(f); cs2.setFont(f2); cs3.setFont(f2); cs4.setFont(f); cs2.setAlignment(CellStyle.ALIGN_LEFT); cs3.setAlignment(CellStyle.ALIGN_RIGHT); cs4.setAlignment(CellStyle.ALIGN_RIGHT); r = s.createRow(lin); for (int i = 1; i < model.getColumnCount(); i++) { c = r.createCell(col); c.setCellStyle(cs); c.setCellValue(model.getColumnName(i)); col = col + 1; } col = 0; for (int i = 0; i < model.getRowCount(); i++) { lin = lin + 1; r = s.createRow(lin); col = 0; for (int j = 1; j < model.getColumnCount(); j++) { String valor = model.getValueAt(i, j).toString(); if (valor == null) { valor = ""; } c = r.createCell(col); c.setCellStyle(cs2); if (valor.matches("-?\\d+(\\.\\d+)?")) { c.setCellValue(Double.parseDouble(valor)); c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellStyle(cs4); } else { c.setCellValue(valor); } col = col + 1; } } for (int i = 0; i <= model.getColumnCount(); i++) { s.autoSizeColumn(i); } wb.write(out); wb.close(); out.close(); Desktop desktop = Desktop.getDesktop(); desktop.open(new File(nameFile)); } catch (FileNotFoundException e) { JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); e.printStackTrace(); } catch (IOException e) { JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); e.printStackTrace(); } return null; }
From source file:br.com.jinsync.controller.ExportExcelFile.java
License:Open Source License
@Override protected Object doInBackground() throws Exception { int valuePgsBar = 0; int qtdTotLin = 0; final String dir = System.getProperty("user.dir") + "\\file"; File arqProp = new File(dir); if (!arqProp.exists()) { arqProp.mkdirs();//from w ww.ja v a 2 s. com } String nameFile = name; int lin = 0; int col = 0; int pos = 0; int posEnd = 0; pos = nameFile.lastIndexOf("("); if (pos > 0) { posEnd = nameFile.lastIndexOf(")"); nameFile = dir + "\\" + nameFile.substring(pos + 1, posEnd) + "_file" + ".xlsx"; } else { pos = nameFile.lastIndexOf("\\"); if (pos > 0) { posEnd = nameFile.lastIndexOf("."); if (posEnd > 0) { nameFile = dir + "\\" + nameFile.substring(pos + 1, posEnd) + "_file" + ".xlsx"; } else { nameFile = dir + nameFile.substring(pos) + "_file" + ".xlsx"; } } } FileOutputStream out; try { out = new FileOutputStream(nameFile); TableModel model = tableName.getModel(); XSSFWorkbook wb = new XSSFWorkbook(); // Criando area de trabalho // para o excel XSSFSheet s = wb.createSheet(); // criando uma nova sheet XSSFFont f = wb.createFont(); XSSFFont f2 = wb.createFont(); XSSFCellStyle cs = wb.createCellStyle(); XSSFCellStyle cs2 = wb.createCellStyle(); XSSFCellStyle cs3 = wb.createCellStyle(); XSSFCellStyle cs4 = wb.createCellStyle(); f.setFontHeightInPoints((short) 8); f2.setFontHeightInPoints((short) 8); f.setBoldweight(Font.BOLDWEIGHT_BOLD); f2.setBoldweight(Font.BOLDWEIGHT_NORMAL); f.setFontName("Courier New"); f2.setFontName("Courier New"); XSSFRow r = null; // Criando uma referencia para Linha XSSFCell c = null; // Referencia para Celula cs.setFont(f); cs2.setFont(f2); cs3.setFont(f2); cs4.setFont(f2); cs2.setAlignment(CellStyle.ALIGN_LEFT); cs3.setAlignment(CellStyle.ALIGN_RIGHT); cs4.setAlignment(CellStyle.ALIGN_RIGHT); r = s.createRow(lin); for (int i = 1; i < model.getColumnCount(); i++) { c = r.createCell(col); c.setCellStyle(cs); c.setCellValue(model.getColumnName(i)); col = col + 1; } col = 0; qtdTotLin = model.getRowCount() - 1; if (qtdTotLin == 0) { qtdTotLin = 1; } for (int i = 0; i < model.getRowCount(); i++) { valuePgsBar = (i * 100) / qtdTotLin; lin = lin + 1; r = s.createRow(lin); col = 0; for (int j = 1; j < model.getColumnCount(); j++) { String valor = model.getValueAt(i, j).toString(); if (valor == null) { valor = ""; } c = r.createCell(col); c.setCellStyle(cs2); valor = valor.trim(); if (valor.matches("-?\\d+(\\.\\d+)?")) { c.setCellValue(Double.parseDouble(valor)); c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellStyle(cs4); } else { c.setCellValue(valor); } col = col + 1; setProgress(valuePgsBar); } } for (int i = 0; i <= model.getColumnCount(); i++) { s.autoSizeColumn(i); } wb.write(out); wb.close(); out.close(); Desktop desktop = Desktop.getDesktop(); desktop.open(new File(nameFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); e.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } return null; }
From source file:br.com.jinsync.controller.ExportExcelString.java
License:Open Source License
@Override protected Object doInBackground() throws Exception { final String dir = System.getProperty("user.dir") + "\\string"; File arqProp = new File(dir); if (!arqProp.exists()) { arqProp.mkdirs();/* w w w .j a v a 2 s. c o m*/ } String nameFile = this.nameFile; int lin = 0; int col = 0; int pos = 0; int posFim = 0; int valDec = 0; pos = nameFile.lastIndexOf("("); if (pos > 0) { posFim = nameFile.lastIndexOf(")"); nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_string" + ".xlsx"; } else { pos = nameFile.lastIndexOf("\\"); if (pos > 0) { posFim = nameFile.lastIndexOf("."); if (posFim > 0) { nameFile = dir + "\\" + nameFile.substring(pos + 1, posFim) + "_string" + ".xlsx"; } else { nameFile = dir + nameFile.substring(pos) + "_string" + ".xlsx"; } } } FileOutputStream out; try { out = new FileOutputStream(nameFile); TableModel model = tableName.getModel(); XSSFWorkbook wb = new XSSFWorkbook(); // Criando area de trabalho // para o excel XSSFSheet s = wb.createSheet(); // criando uma nova sheet XSSFFont f = wb.createFont(); XSSFFont f2 = wb.createFont(); XSSFCellStyle cs = wb.createCellStyle(); XSSFCellStyle cs2 = wb.createCellStyle(); XSSFCellStyle cs3 = wb.createCellStyle(); XSSFCellStyle cs4 = wb.createCellStyle(); f.setFontHeightInPoints((short) 8); f2.setFontHeightInPoints((short) 8); f.setBoldweight(Font.BOLDWEIGHT_BOLD); f2.setBoldweight(Font.BOLDWEIGHT_NORMAL); f.setFontName("Courier New"); f2.setFontName("Courier New"); XSSFRow r = null; // Criando uma referencia para Linha XSSFCell c = null; // Referencia para Celula cs.setFont(f); cs2.setFont(f2); cs3.setFont(f2); cs4.setFont(f); cs2.setAlignment(CellStyle.ALIGN_LEFT); cs3.setAlignment(CellStyle.ALIGN_RIGHT); cs4.setAlignment(CellStyle.ALIGN_RIGHT); r = s.createRow(lin); r = s.createRow(0); // Criando a primeira linha na LINHA zero, que // seria o nmero 1 c = r.createCell(0); // Criando a celula na posicao ZERO, que seria // A, com referencia na linha zero acima = // A1 c.setCellStyle(cs); c.setCellValue(Language.stringFieldName); c = r.createCell(1); c.setCellStyle(cs); c.setCellValue(Language.stringType); c = r.createCell(2); c.setCellStyle(cs); c.setCellValue(Language.stringLength); c = r.createCell(3); c.setCellStyle(cs); c.setCellValue(Language.stringDecimal); c = r.createCell(4); c.setCellStyle(cs); c.setCellValue(Language.stringTotal); c = r.createCell(5); c.setCellStyle(cs4); c.setCellValue(Language.stringContent); col = 0; lin = lin + 1; for (int i = 0; i < model.getColumnCount(); i++) { r = s.createRow(lin); c = r.createCell(col); c.setCellStyle(cs2); c.setCellValue(model.getColumnName(i)); col = col + 1; c = r.createCell(col); c.setCellStyle(cs2); c.setCellValue(tipoConteudo.get(i)); col = col + 1; c = r.createCell(col); c.setCellStyle(cs2); if (tamanhoConteudo.get(i).matches("-?\\d+(\\.\\d+)?")) { c.setCellValue(Double.parseDouble(tamanhoConteudo.get(i))); c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellStyle(cs3); } else { c.setCellValue(tamanhoConteudo.get(i)); } col = col + 1; c = r.createCell(col); c.setCellStyle(cs2); if (decimalConteudo.get(i).matches("-?\\d+(\\.\\d+)?")) { c.setCellValue(Integer.parseInt(decimalConteudo.get(i))); valDec = Integer.parseInt(decimalConteudo.get(i)); c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellStyle(cs3); } else { c.setCellValue(decimalConteudo.get(i)); valDec = 0; } col = col + 1; c = r.createCell(col); c.setCellStyle(cs2); c.setCellValue(totalConteudo.get(i)); col = col + 1; c = r.createCell(col); c.setCellStyle(cs2); String valor = model.getValueAt(0, i).toString().trim(); if (valor == null) { valor = ""; } if (valor.matches("-?\\d+(\\.\\d+)?")) { if (Double.parseDouble(valor) > 0 && valDec > 0) { double val2 = Double.parseDouble(valor) / (Math.pow(10, valDec)); NumberFormat format = NumberFormat.getInstance(); format.setMinimumFractionDigits(2); format.setMaximumFractionDigits(valDec); c.setCellValue(format.format(val2).toString()); } else { c.setCellValue(Double.parseDouble(valor)); } // c.setCellType(Cell.CELL_TYPE_NUMERIC); c.setCellStyle(cs3); } else { c.setCellValue(valor); } lin = lin + 1; col = 0; } for (int i = 0; i <= model.getColumnCount(); i++) { s.autoSizeColumn(i); } wb.write(out); wb.close(); out.close(); Desktop desktop = Desktop.getDesktop(); desktop.open(new File(nameFile)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); //e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block JOptionPane.showMessageDialog(null, e.getLocalizedMessage()); //e.printStackTrace(); } return null; }
From source file:ch.admin.isb.hermes5.business.userszenario.projektstrukturplan.ProjektstrukturplanGeneratorExcel.java
License:Apache License
private void autosize(XSSFSheet sheet) { sheet.autoSizeColumn(MAIN_COL); sheet.autoSizeColumn(VERANTWORTLICH_COL); }
From source file:com.accounting.accountMBean.DifferentAccReports.java
public void simplePostProcessXLSX(Object document) { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate4"); String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate5"); XSSFWorkbook wb = (XSSFWorkbook) document; XSSFCellStyle headerCellStyle = wb.createCellStyle(); XSSFCellStyle headerCellStyle1 = wb.createCellStyle(); XSSFCellStyle headerCellStyle2 = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); Font headerFont1 = wb.createFont(); headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING); headerFont1.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle1.setFont(headerFont); headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT); Font headerFont3 = wb.createFont(); headerFont3.setBoldweight(Font.U_SINGLE); headerFont3.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle2.setFont(headerFont1); headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); XSSFSheet sheet = wb.getSheetAt(0); int noOfColumns = sheet.getRow(2).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); }// w ww.jav a2 s. co m sheet.shiftRows(0, sheet.getLastRowNum(), 4); XSSFRow firstRow = sheet.createRow(1); firstRow.createCell(0).setCellValue("SALES AGEING REPORT"); firstRow.getCell(0).setCellStyle(headerCellStyle); XSSFRow secondRow = sheet.createRow(0); secondRow.createCell(0).setCellValue(getLoggedInOffice().getName()); secondRow.getCell(0).setCellStyle(headerCellStyle); // XSSFRow thirdRow = sheet.createRow(3); String date = ndc.convertToNepaliDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); String time = sdf.format(new Date()); thirdRow.createCell(0) .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName()); thirdRow.getCell(0).setCellStyle(headerCellStyle2); XSSFRow fourthRow = sheet.createRow(2); fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString); fourthRow.getCell(0).setCellStyle(headerCellStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7)); }
From source file:com.accounting.reportMBean.ReportCommonBean.java
public void simplePostProcessXLSXOfStock(Object document) { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate4"); String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate1"); XSSFWorkbook wb = (XSSFWorkbook) document; XSSFCellStyle headerCellStyle = wb.createCellStyle(); XSSFCellStyle headerCellStyle1 = wb.createCellStyle(); XSSFCellStyle headerCellStyle2 = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); Font headerFont1 = wb.createFont(); headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING); headerFont1.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle1.setFont(headerFont); headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT); Font headerFont3 = wb.createFont(); headerFont3.setBoldweight(Font.U_SINGLE); headerFont3.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle2.setFont(headerFont1); headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); XSSFSheet sheet = wb.getSheetAt(0); int noOfColumns = sheet.getRow(2).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); }/*from w ww . ja v a 2 s .c o m*/ sheet.shiftRows(0, sheet.getLastRowNum(), 4); XSSFRow firstRow = sheet.createRow(1); firstRow.createCell(0).setCellValue("STOCK VALUE WITH VALUES"); firstRow.getCell(0).setCellStyle(headerCellStyle); XSSFRow secondRow = sheet.createRow(0); secondRow.createCell(0).setCellValue(getLoggedInOffice().getName()); secondRow.getCell(0).setCellStyle(headerCellStyle); // XSSFRow thirdRow = sheet.createRow(3); String date = ndc.convertToNepaliDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); String time = sdf.format(new Date()); thirdRow.createCell(0) .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName()); thirdRow.getCell(0).setCellStyle(headerCellStyle2); XSSFRow fourthRow = sheet.createRow(2); fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString); fourthRow.getCell(0).setCellStyle(headerCellStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7)); }
From source file:com.accounting.reportMBean.ReportCommonBean.java
public void simplePostProcessXLSX(Object document) { String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate4"); String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap() .get("nepaliDate1"); XSSFWorkbook wb = (XSSFWorkbook) document; XSSFCellStyle headerCellStyle = wb.createCellStyle(); XSSFCellStyle headerCellStyle1 = wb.createCellStyle(); XSSFCellStyle headerCellStyle2 = wb.createCellStyle(); Font headerFont = wb.createFont(); headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD); headerFont.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER); Font headerFont1 = wb.createFont(); headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING); headerFont1.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle1.setFont(headerFont); headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT); Font headerFont3 = wb.createFont(); headerFont3.setBoldweight(Font.U_SINGLE); headerFont3.setFontName(HSSFFont.FONT_ARIAL); headerCellStyle2.setFont(headerFont1); headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT); XSSFSheet sheet = wb.getSheetAt(0); int noOfColumns = sheet.getRow(2).getLastCellNum(); for (int i = 0; i < noOfColumns; i++) { sheet.autoSizeColumn(i); }//from w w w .j a v a 2s.c om sheet.shiftRows(0, sheet.getLastRowNum(), 4); XSSFRow firstRow = sheet.createRow(1); firstRow.createCell(0).setCellValue(pageName); firstRow.getCell(0).setCellStyle(headerCellStyle); XSSFRow secondRow = sheet.createRow(0); secondRow.createCell(0).setCellValue(getLoggedInOffice().getName()); secondRow.getCell(0).setCellStyle(headerCellStyle); // XSSFRow thirdRow = sheet.createRow(3); String date = ndc.convertToNepaliDate(new Date()); SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a"); String time = sdf.format(new Date()); thirdRow.createCell(0) .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName()); thirdRow.getCell(0).setCellStyle(headerCellStyle2); XSSFRow fourthRow = sheet.createRow(2); fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString); fourthRow.getCell(0).setCellStyle(headerCellStyle); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7)); sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7)); }
From source file:com.anritsu.mcrepositorymanager.utils.GenerateRSS.java
public String getRSS() { FileInputStream file = null;/* w w w . j av a 2 s . c om*/ String rssFileName = rssTemplateFileName.replaceAll("template", mcVersion); try { file = new FileInputStream( new File(Configuration.getInstance().getRssTemplatePath() + rssTemplateFileName)); XSSFWorkbook workbook = new XSSFWorkbook(file); workbook.setSheetName(workbook.getSheetIndex("MC X.X.X"), "MC " + mcVersion); XSSFSheet sheet = workbook.getSheet("MC " + mcVersion); CreationHelper createHelper = workbook.getCreationHelper(); Cell cell = null; // Update the sheet title cell = sheet.getRow(0).getCell(0); cell.setCellValue(cell.getStringCellValue().replaceAll("template", mcVersion)); XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); cellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); XSSFCellStyle hlinkstyle = workbook.createCellStyle(); XSSFFont hlinkfont = workbook.createFont(); hlinkfont.setUnderline(XSSFFont.U_SINGLE); hlinkfont.setColor(HSSFColor.BLUE.index); hlinkstyle.setFont(hlinkfont); hlinkstyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); hlinkstyle.setBorderTop(XSSFCellStyle.BORDER_THIN); hlinkstyle.setBorderRight(XSSFCellStyle.BORDER_THIN); hlinkstyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); XSSFCellStyle dateCellStyle = workbook.createCellStyle(); dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MMMM-yyyy")); dateCellStyle.setBorderBottom(XSSFCellStyle.BORDER_THIN); dateCellStyle.setBorderTop(XSSFCellStyle.BORDER_THIN); dateCellStyle.setBorderRight(XSSFCellStyle.BORDER_THIN); dateCellStyle.setBorderLeft(XSSFCellStyle.BORDER_THIN); // Populate the table int rowCount = 1; for (RecommendedMcPackage rmcp : sortedMcPackages) { if (rmcp.getRecommendedVersion() != null && rmcp.isShowInTable()) { Row row = sheet.createRow(rowCount + 1); rowCount++; cell = row.createCell(0); cell.setCellValue(rmcp.getTier().replaceAll("Anritsu/MasterClaw/", "")); cell.setCellStyle(cellStyle); cell = row.createCell(1); cell.setCellValue(rmcp.getGroup()); cell.setCellStyle(cellStyle); cell = row.createCell(2); cell.setCellValue(rmcp.getPackageName()); UrlValidator defaultValidator = new UrlValidator(UrlValidator.ALLOW_LOCAL_URLS); if (rmcp.getRecommendedVersion().getReleaseNote() != null && defaultValidator.isValid(rmcp.getRecommendedVersion().getReleaseNote())) { XSSFHyperlink releaseNotelink = (XSSFHyperlink) createHelper .createHyperlink(Hyperlink.LINK_URL); releaseNotelink.setAddress(rmcp.getRecommendedVersion().getReleaseNote()); //System.out.println("Inside(if) RN: " + rmcp.getRecommendedVersion().getReleaseNote() + " Valid: " + defaultValidator.isValid(rmcp.getRecommendedVersion().getReleaseNote())); cell.setHyperlink(releaseNotelink); } cell.setCellStyle(hlinkstyle); cell = row.createCell(3); cell.setCellValue(rmcp.getRecommendedVersion().getPackageVersion()); cell.setCellStyle(cellStyle); cell = row.createCell(4); cell.setCellValue(rmcp.getAvailability()); cell.setCellStyle(cellStyle); cell = row.createCell(5); String customers = Arrays.asList(rmcp.getRecommendedVersion().getCustomerList().toArray()) .toString(); if (customers.equalsIgnoreCase("[All]")) { customers = ""; } cell.setCellValue(customers); cell.setCellStyle(cellStyle); cell = row.createCell(6); cell.setCellValue(rmcp.getRecommendedVersion().getRisk()); cell.setCellStyle(cellStyle); cell = row.createCell(7); cell.setCellValue(rmcp.getPackageName()); XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL); link.setAddress(rmcp.getRecommendedVersion().getDownloadLinks().iterator().next()); cell.setHyperlink((XSSFHyperlink) link); cell.setCellStyle(hlinkstyle); cell = row.createCell(8); cell.setCellValue((rmcp.getRecommendedVersion() != null && rmcp.getRecommendedVersion().isLessRecommended()) ? "#" : ""); cell.setCellStyle(cellStyle); cell = row.createCell(9); cell.setCellValue(rmcp.getRecommendedVersion().getNotes()); cell.setCellStyle(cellStyle); StringBuilder newFeatures = new StringBuilder(); for (MCPackageActivities mcpa : rmcp.getRecommendedVersion().getActivities()) { if (!mcpa.getActivityType().equalsIgnoreCase("epr")) { newFeatures.append(mcpa.getActivityType() + " " + mcpa.getActivityId() + "; "); } } cell = row.createCell(10); cell.setCellValue(newFeatures.toString()); cell.setCellStyle(cellStyle); cell = row.createCell(11); cell.setCellValue(rmcp.getRecommendedVersion().getReleaseDate()); cell.setCellStyle(dateCellStyle); } sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); sheet.autoSizeColumn(2); sheet.autoSizeColumn(3); sheet.autoSizeColumn(4); sheet.autoSizeColumn(6); sheet.autoSizeColumn(7); sheet.autoSizeColumn(8); sheet.autoSizeColumn(11); } FileOutputStream outFile = new FileOutputStream( new File(Configuration.getInstance().getRssTemplatePath() + rssFileName)); workbook.write(outFile); outFile.close(); return Configuration.getInstance().getRssTemplatePath() + rssFileName; } catch (FileNotFoundException ex) { Logger.getLogger(GenerateRSS.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(GenerateRSS.class.getName()).log(Level.SEVERE, null, ex); } finally { try { file.close(); } catch (IOException ex) { Logger.getLogger(GenerateRSS.class.getName()).log(Level.SEVERE, null, ex); } } return ""; }
From source file:com.frameworkset.platform.util.POIExcelUtil.java
License:Open Source License
/** * Excel Workbook?.//from w w w. j a v a 2s. c o m * * @param colDesc * "?:user_id,??:user_name,:type_name" * @param dataList * @return * @author gw_liaozh * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static XSSFWorkbook createHSSFWorkbook(List<String> titlesList, List<?> dataList) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { // ??? List<String> colFieldList = getColumnFieldList(titlesList); XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); XSSFFont font = getBaseFont(wb); XSSFCellStyle headCellStyle = getHeadCellStyle(wb, font); // ? CellStyle dateCellStyle = getDateTimeCellStyle(wb); // ??1717 XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet); Map<String, Class<?>> fieldTypeMap = new HashMap<String, Class<?>>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); // XSSFRow titleRow = sheet.createRow(0); for (int i = 0; i < titlesList.size(); i++) { XSSFCell cell = titleRow.createCell(i); cell.setCellStyle(headCellStyle); cell.setCellType(HSSFCell.CELL_TYPE_STRING); cell.setCellValue(titlesList.get(i)); } ClassInfo classInfo = null; // ? for (int i = 0; i < dataList.size(); i++) { Object obj = dataList.get(i); if (classInfo == null) classInfo = ClassUtil.getClassInfo(obj.getClass()); XSSFRow row = sheet.createRow(i + 1); for (int j = 0; j < colFieldList.size(); j++) { String fieldName = colFieldList.get(j); XSSFCell cell = row.createCell(j); if (obj == null) { continue; } PropertieDescription reflexField = classInfo.getPropertyDescriptor(fieldName); Object value = reflexField.getValue(obj); // ClassInfo classInfo = ClassUtil.getClassInfo(obj.getClass()); // Object value = classInfo.getPropertyDescriptor(fieldName).getValue(obj); if (value == null) { continue; } // ?? if (value instanceof Number) { cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC); cell.setCellValue(((Number) value).doubleValue()); } else if (value instanceof Date || value instanceof Timestamp) { cell.setCellStyle(dateCellStyle); cell.setCellValue(sdf.format((Date) value)); } else { cell.setCellType(HSSFCell.CELL_TYPE_STRING); // cell.setCellStyle(strCellStyle); cell.setCellValue(value.toString()); } fieldTypeMap.put(fieldName, value.getClass()); } } // ?? for (int i = 0; i < colFieldList.size(); i++) { String fieldName = colFieldList.get(i); Class<?> fieldClass = fieldTypeMap.get(fieldName); if (fieldClass == null) { continue; } CellRangeAddressList range = new CellRangeAddressList(1, 65535, i, i); DataValidationConstraint constraint = null; if (Integer.class.isAssignableFrom(fieldClass)) { constraint = dvHelper.createIntegerConstraint(DataValidationConstraint.OperatorType.NOT_BETWEEN, "0", "-1"); sheet.addValidationData(dvHelper.createValidation(constraint, range)); } else if (Number.class.isAssignableFrom(fieldClass)) { constraint = dvHelper.createNumericConstraint(DataValidationConstraint.ValidationType.DECIMAL, DataValidationConstraint.OperatorType.NOT_BETWEEN, "0", "-1"); sheet.addValidationData(dvHelper.createValidation(constraint, range)); } else if (Date.class.isAssignableFrom(fieldClass)) { constraint = dvHelper.createDateConstraint(DataValidationConstraint.OperatorType.NOT_BETWEEN, "0000-01-02", "0000-01-01", "yyyy-MM-dd"); sheet.addValidationData(dvHelper.createValidation(constraint, range)); } } // for (int i = 0; i < titlesList.size(); i++) { // ?? sheet.autoSizeColumn(i); } return wb; }
From source file:com.pe.nisira.movil.view.action.MultitablaAction.java
public StreamedContent downFormatExcel() throws Exception { InputStream stream = null;/* ww w. ja va 2 s . co m*/ StreamedContent arch = null; try { String folder = "C:\\SOLUTION\\WEB\\FORMATOS_IMPORTACION"; File ruta = new File(folder); if (!ruta.isDirectory()) { ruta.mkdirs(); } String rutaArchivo = folder + "\\FI_MULTITABLA.xlsx"; File fileXls = new File(rutaArchivo); if (fileXls.exists()) { fileXls.delete(); } fileXls.createNewFile(); XSSFWorkbook libro = new XSSFWorkbook(); FileOutputStream file = new FileOutputStream(fileXls); XSSFSheet hoja = libro.createSheet("IMPORTAR_MULTITABLA"); CreationHelper factory = libro.getCreationHelper(); hoja = libro.getSheetAt(0); XSSFCellStyle style = libro.createCellStyle(); Font font = libro.createFont(); Font font1 = libro.createFont(); Drawing drawing = hoja.createDrawingPatriarch(); ClientAnchor anchor1 = factory.createClientAnchor(); font.setBoldweight(Font.BOLDWEIGHT_BOLD); font.setFontHeightInPoints((short) 8); font1.setFontHeightInPoints((short) 8); font1.setFontName("Arial"); font.setFontName("Arial"); style.setFillForegroundColor(new XSSFColor(new java.awt.Color(247, 150, 70))); style.setFillPattern(CellStyle.SOLID_FOREGROUND); style.setAlignment(CellStyle.VERTICAL_CENTER); style.setAlignment(CellStyle.ALIGN_CENTER); style.setFont(font); for (int f = 0; f < 1; f++) { XSSFRow fila = hoja.createRow(f); for (int c = 0; c < 4; c++) { XSSFCell celda = fila.createCell(c); celda.setCellStyle(style); anchor1.setCol1(celda.getColumnIndex()); anchor1.setCol2(celda.getColumnIndex() + 4); anchor1.setRow1(fila.getRowNum()); anchor1.setRow2(fila.getRowNum() + 3); Comment comment = drawing.createCellComment(anchor1); if (f == 0 && c == 0) { RichTextString str = factory.createRichTextString( "ADM:\nCampo Obligatorio \n - Indicar si es es Padre (Usar SI o NO)."); str.applyFont(font1); str.applyFont(0, 29, font); comment.setString(str); comment.setAuthor("ADM"); celda.setCellValue("Es Padre"); celda.setCellComment(comment); } else if (f == 0 && c == 1) { RichTextString str = factory.createRichTextString( "ADM:\nCampo Opcional \n - Escribir la Abreviatura del campo del cual depende este."); str.applyFont(font1); str.applyFont(0, 29, font); comment.setString(str); comment.setAuthor("ADM"); celda.setCellValue("Abreviatura Padre"); celda.setCellComment(comment); } else if (f == 0 && c == 2) { RichTextString str = factory .createRichTextString("ADM:\nCampo Obligatorio \n - Descripcion de la multitabla"); str.applyFont(font1); str.applyFont(0, 29, font); comment.setString(str); comment.setAuthor("ADM"); celda.setCellValue("DESCRIPCION"); celda.setCellComment(comment); } else if (f == 0 && c == 3) { RichTextString str = factory .createRichTextString("ADM:\nCampo Obligatorio \n - Abreviatura de la multitabla."); str.applyFont(font1); str.applyFont(0, 29, font); comment.setString(str); comment.setAuthor("ADM"); celda.setCellValue("ABREVIATURA"); celda.setCellComment(comment); } } } hoja.autoSizeColumn((short) 0); hoja.autoSizeColumn((short) 1); hoja.autoSizeColumn((short) 2); libro.write(file); file.close(); stream = new FileInputStream(new File(rutaArchivo)); arch = new DefaultStreamedContent(stream, "application/xlsx", "FI_MULTITABLA.xlsx"); } catch (FileNotFoundException ex) { System.out.println("Error al Descargar : " + ex.getMessage()); } return arch; }