private void printSetup(Sheet oldSheet, Sheet newSheet) { // print setup PrintSetup oldPS = oldSheet.getPrintSetup(); PrintSetup newPS = newSheet.getPrintSetup(); newPS.setPaperSize(oldPS.getPaperSize()); newPS.setScale(oldPS.getScale()); newPS.setPageStart(oldPS.getPageStart()); newPS.setFitWidth(oldPS.getFitWidth()); newPS.setFitHeight(oldPS.getFitHeight()); newPS.setLeftToRight(oldPS.getLeftToRight()); newPS.setLandscape(oldPS.getLandscape()); newPS.setValidSettings(oldPS.getValidSettings()); newPS.setNoColor(oldPS.getNoColor()); newPS.setDraft(oldPS.getDraft()); newPS.setNotes(oldPS.getNotes()); newPS.setNoOrientation(oldPS.getNoOrientation()); newPS.setUsePage(oldPS.getUsePage()); newPS.setHResolution(oldPS.getHResolution()); newPS.setVResolution(oldPS.getVResolution()); newPS.setHeaderMargin(oldPS.getHeaderMargin()); newPS.setFooterMargin(oldPS.getFooterMargin()); newPS.setCopies(oldPS.getCopies()); newSheet.setRightToLeft(oldSheet.isRightToLeft()); newSheet.setHorizontallyCenter(oldSheet.getHorizontallyCenter()); newSheet.setVerticallyCenter(oldSheet.getVerticallyCenter()); newSheet.setAutobreaks(oldSheet.getAutobreaks()); newSheet.setDisplayZeros(oldSheet.isDisplayZeros()); newSheet.setFitToPage(oldSheet.getFitToPage()); newSheet.setAutobreaks(oldSheet.getAutobreaks()); newSheet.setPrintGridlines(oldSheet.isPrintGridlines()); newSheet.getHeader().setCenter(oldSheet.getHeader().getCenter()); newSheet.getHeader().setLeft(oldSheet.getHeader().getLeft()); newSheet.getHeader().setRight(oldSheet.getHeader().getRight()); newSheet.getFooter().setCenter(oldSheet.getFooter().getCenter()); newSheet.getFooter().setLeft(oldSheet.getFooter().getLeft()); newSheet.getFooter().setRight(oldSheet.getFooter().getRight()); newSheet.setMargin(Sheet.LeftMargin, oldSheet.getMargin(Sheet.LeftMargin)); newSheet.setMargin(Sheet.RightMargin, oldSheet.getMargin(Sheet.RightMargin)); newSheet.setMargin(Sheet.HeaderMargin, oldSheet.getMargin(Sheet.HeaderMargin)); newSheet.setMargin(Sheet.FooterMargin, oldSheet.getMargin(Sheet.FooterMargin)); newSheet.setMargin(Sheet.BottomMargin, oldSheet.getMargin(Sheet.BottomMargin)); newSheet.setMargin(Sheet.TopMargin, oldSheet.getMargin(Sheet.TopMargin)); }
// Define A method for Creating Excel File public String createExcel( String titleString, String sheetString, String subTitleString, List data, String[] headerTitles, String titleKey, String filePath) { String file = null; Workbook wb; int col = 0; try { // check Header Title if (headerTitles != null && headerTitles.length > 0) col = headerTitles.length; wb = (Workbook) new HSSFWorkbook(); // Hear we are getting whole property List<ConfigurationUtilBean> titleMap = new CustomerCommonPropertyMap().getTitles(titleKey); Map<String, CellStyle> styles = createStyles(wb); Sheet sheet = wb.createSheet(sheetString); PrintSetup printSetup = sheet.getPrintSetup(); printSetup.setLandscape(true); sheet.setFitToPage(true); sheet.setHorizontallyCenter(true); Header header = sheet.getHeader(); header.setCenter("Center Header"); header.setLeft("Left Header"); header.setRight("Right Footer"); Footer footer = sheet.getFooter(); footer.setCenter("center footer"); footer.setLeft("left footer"); footer.setRight("right footer"); // Title Row.... Row titleRow = sheet.createRow(0); titleRow.setHeightInPoints(20); Cell titleCell = titleRow.createCell(0); titleCell.setCellValue(titleString); titleCell.setCellStyle(styles.get("title")); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, col - 1)); // Sub Title Row..... // System.out.println("Sub Title String >>>>>>"+subTitleString); Row headerRow = null; if (subTitleString != "") { Row subTitleRow = sheet.createRow(1); subTitleRow.setHeightInPoints(18); Cell subTitleCell = subTitleRow.createCell(0); subTitleCell.setCellValue(subTitleString); subTitleCell.setCellStyle(styles.get("subTitle")); sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, col - 1)); headerRow = sheet.createRow(2); headerRow.setHeightInPoints(15); // } else { headerRow = sheet.createRow(1); headerRow.setHeightInPoints(15); } Cell headerCell = null; if (headerTitles != null) { for (ConfigurationUtilBean cell : titleMap) { int titleIndex = 0; for (int i = 0; i < headerTitles.length; i++) { if (cell.getKey().equalsIgnoreCase(headerTitles[titleIndex].trim())) { headerCell = headerRow.createCell(titleIndex); headerCell.setCellValue(cell.getValue()); headerCell.setCellStyle(styles.get("header")); } titleIndex++; } } } Row dataRow = null; Cell dataCell = null; int rowIndex = 2; /* List Iteration text */ try { if (data != null && data.size() > 0) { for (Iterator it = data.iterator(); it.hasNext(); ) { Object[] obdata = (Object[]) it.next(); dataRow = sheet.createRow(rowIndex); for (int cellIndex = 0; cellIndex < headerTitles.length; cellIndex++) { dataCell = dataRow.createCell(cellIndex); if (obdata[cellIndex] != null && !obdata[cellIndex].toString().equalsIgnoreCase("")) { dataCell.setCellValue(obdata[cellIndex].toString()); } else { dataCell.setCellValue("NA"); } } rowIndex++; } } } catch (Exception e) { // TODO: handle exception } for (int titleIndex = 0; titleIndex < headerTitles.length; titleIndex++) sheet.autoSizeColumn(titleIndex); // adjust width of the column file = filePath + File.separator + "OpportunityReportDetail_" + DateUtil.getCurrentDateIndianFormat() + (DateUtil.getCurrentTimeHourMin()).replaceAll(":", "-") + ".xls"; if (wb instanceof XSSFWorkbook) file += "x"; FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { } return file; }
// Define A method for Creating Excel File public String createExcelformate( String titleString, String sheetString, String[] headerTitles, String titleKey, String filePath) { String file = null; Workbook wb; int col = 0; try { // check Header Title if (headerTitles != null && headerTitles.length > 0) col = headerTitles.length; wb = (Workbook) new HSSFWorkbook(); // Hear we are getting whole property List<ConfigurationUtilBean> titleMap = new CustomerCommonPropertyMap().getTitles(titleKey); Map<String, CellStyle> styles = createStyles(wb); Sheet sheet = wb.createSheet(sheetString); PrintSetup printSetup = sheet.getPrintSetup(); printSetup.setLandscape(true); sheet.setFitToPage(true); sheet.setHorizontallyCenter(true); Header header = sheet.getHeader(); header.setCenter("Center Header"); header.setLeft("Left Header"); header.setRight("Right Footer"); Footer footer = sheet.getFooter(); footer.setCenter("center footer"); footer.setLeft("left footer"); footer.setRight("right footer"); // Title Row.... Row titleRow = sheet.createRow(0); titleRow.setHeightInPoints(20); Cell titleCell = titleRow.createCell(0); titleCell.setCellValue(titleString); titleCell.setCellStyle(styles.get("title")); sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, col - 1)); // Row headerRow = sheet.createRow(1); headerRow.setHeightInPoints(15); Cell headerCell = null; if (headerTitles != null) { for (ConfigurationUtilBean cell : titleMap) { int titleIndex = 0; for (int i = 0; i < headerTitles.length; i++) { if (cell.getKey().equalsIgnoreCase(headerTitles[titleIndex].trim())) { headerCell = headerRow.createCell(titleIndex); headerCell.setCellValue(cell.getValue()); headerCell.setCellStyle(styles.get("header")); } titleIndex++; } } } for (int titleIndex = 0; titleIndex < headerTitles.length; titleIndex++) sheet.autoSizeColumn(titleIndex); // adjust width of the column file = filePath + File.separator + "ContactReport" + DateUtil.getCurrentDateIndianFormat() + (DateUtil.getCurrentTime()).replaceAll(":", "-") + ".xls"; if (wb instanceof XSSFWorkbook) file += "x"; FileOutputStream out = new FileOutputStream(file); wb.write(out); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { } return file; }