List of usage examples for org.apache.poi.ss.usermodel CellStyle setFont
void setFont(Font font);
From source file:com.jeans.iservlet.controller.impl.ExportController.java
/** * ????/*from w w w .j a va 2 s . c om*/ * * @param ids * ???(AccessoryEntryInvoice.id)? * @return * @throws IOException */ private void exportAccessoryEntries(List<Long> idList) throws IOException { StringBuilder fn = new StringBuilder(getCurrentCompany().getName()); fn.append(" - ????.xlsx"); Workbook wb = new XSSFWorkbook(); DataFormat df = wb.createDataFormat(); // ?10?? Font bFont = wb.createFont(); bFont.setBoldweight(Font.BOLDWEIGHT_BOLD); bFont.setFontName(""); bFont.setFontHeightInPoints((short) 10); // ?10? Font nFont = wb.createFont(); nFont.setFontName(""); nFont.setFontHeightInPoints((short) 10); // ??1??????? CellStyle csLeftTextBold = wb.createCellStyle(); csLeftTextBold.setAlignment(CellStyle.ALIGN_LEFT); csLeftTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTextBold.setFont(bFont); csLeftTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftTextBold.setWrapText(false); // ??2??????? CellStyle csLeftText = wb.createCellStyle(); csLeftText.setAlignment(CellStyle.ALIGN_LEFT); csLeftText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftText.setFont(nFont); csLeftText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftText.setWrapText(false); // ??3??????? CellStyle csLeftTime = wb.createCellStyle(); csLeftTime.setAlignment(CellStyle.ALIGN_LEFT); csLeftTime.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTime.setFont(nFont); csLeftTime.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm")); csLeftTime.setWrapText(false); // ??4??????? CellStyle csCenterTextBold = wb.createCellStyle(); csCenterTextBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterTextBold.setFont(bFont); csCenterTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterTextBold.setWrapText(false); // ??5??????? CellStyle csCenterText = wb.createCellStyle(); csCenterText.setAlignment(CellStyle.ALIGN_CENTER); csCenterText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterText.setFont(nFont); csCenterText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterText.setWrapText(false); // ??6??????? CellStyle csCenterNumber = wb.createCellStyle(); csCenterNumber.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumber.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumber.setFont(nFont); csCenterNumber.setDataFormat(df.getFormat("#")); csCenterNumber.setWrapText(false); // ??7????8???? CellStyle csLeftId = wb.createCellStyle(); csLeftId.setAlignment(CellStyle.ALIGN_LEFT); csLeftId.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftId.setFont(nFont); csLeftId.setDataFormat(df.getFormat("#00000000")); csLeftId.setWrapText(false); // ??8??????? CellStyle csCenterNumberBold = wb.createCellStyle(); csCenterNumberBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumberBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumberBold.setFont(bFont); csCenterNumberBold.setDataFormat(df.getFormat("#")); csCenterNumberBold.setWrapText(false); // ???sheet for (long id : idList) { AccessoryEntryInvoice invoice = arService.loadEntryInvoice(id); if (null == invoice) { continue; } Sheet sheet = wb.createSheet("?#" + id); // ??xxxxxx Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("??"); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftId); cell.setCellValue(id); // yyyy-MM-dd HH:mm row = sheet.createRow(1); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftTime); cell.setCellValue(invoice.getTime()); // xxx row = sheet.createRow(2); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(invoice.getOperator().getName()); // row = sheet.createRow(3); row.setHeightInPoints(20); for (int i = 0; i < 8; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue(ENT_HEADERS[i]); sheet.setColumnWidth(i, ENT_HEADERS_WIDTH[i] * 256); } // List<AccessoryEntry> entries = invoice.getEntries(); int rowNumber = 4; int totalQuantity = 0; int totalRemained = 0; for (AccessoryEntry entry : entries) { Accessory acs = entry.getAccessory(); row = sheet.createRow(rowNumber); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getType().getTitle()); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getName()); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getBrand()); cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getModel()); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getDescription()); cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(entry.getQuantity()); totalQuantity += entry.getQuantity(); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getUnit()); cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(entry.getRemained()); totalRemained += entry.getRemained(); rowNumber++; } // ? row = sheet.createRow(rowNumber); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue("?"); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(entries.size()); cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalQuantity); cell = row.createCell(7, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalRemained); } String filename = null; if (isIE()) { filename = URLEncoder.encode(fn.toString(), "UTF-8").replaceAll("\\+", "%20"); } else { filename = new String(fn.toString().getBytes("UTF-8"), "iso8859-1"); } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 4096); wb.write(out); wb.close(); out.close(); }
From source file:com.jeans.iservlet.controller.impl.ExportController.java
/** * ????//from ww w . j a v a2 s. com * * @param ids * ???(AccessoryDischargeInvoice.id)? * @return * @throws IOException */ private void exportAccessoryDischarges(List<Long> idList) throws IOException { StringBuilder fn = new StringBuilder(getCurrentCompany().getName()); fn.append(" - ????.xlsx"); Workbook wb = new XSSFWorkbook(); DataFormat df = wb.createDataFormat(); // ?10?? Font bFont = wb.createFont(); bFont.setBoldweight(Font.BOLDWEIGHT_BOLD); bFont.setFontName(""); bFont.setFontHeightInPoints((short) 10); // ?10? Font nFont = wb.createFont(); nFont.setFontName(""); nFont.setFontHeightInPoints((short) 10); // ??1??????? CellStyle csLeftTextBold = wb.createCellStyle(); csLeftTextBold.setAlignment(CellStyle.ALIGN_LEFT); csLeftTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTextBold.setFont(bFont); csLeftTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftTextBold.setWrapText(false); // ??2??????? CellStyle csLeftText = wb.createCellStyle(); csLeftText.setAlignment(CellStyle.ALIGN_LEFT); csLeftText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftText.setFont(nFont); csLeftText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftText.setWrapText(false); // ??3??????? CellStyle csLeftTime = wb.createCellStyle(); csLeftTime.setAlignment(CellStyle.ALIGN_LEFT); csLeftTime.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTime.setFont(nFont); csLeftTime.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm")); csLeftTime.setWrapText(false); // ??4??????? CellStyle csCenterTextBold = wb.createCellStyle(); csCenterTextBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterTextBold.setFont(bFont); csCenterTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterTextBold.setWrapText(false); // ??5??????? CellStyle csCenterText = wb.createCellStyle(); csCenterText.setAlignment(CellStyle.ALIGN_CENTER); csCenterText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterText.setFont(nFont); csCenterText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterText.setWrapText(false); // ??6??????? CellStyle csCenterNumber = wb.createCellStyle(); csCenterNumber.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumber.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumber.setFont(nFont); csCenterNumber.setDataFormat(df.getFormat("#")); csCenterNumber.setWrapText(false); // ??7????8???? CellStyle csLeftId = wb.createCellStyle(); csLeftId.setAlignment(CellStyle.ALIGN_LEFT); csLeftId.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftId.setFont(nFont); csLeftId.setDataFormat(df.getFormat("#00000000")); csLeftId.setWrapText(false); // ??8??????? CellStyle csCenterNumberBold = wb.createCellStyle(); csCenterNumberBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumberBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumberBold.setFont(bFont); csCenterNumberBold.setDataFormat(df.getFormat("#")); csCenterNumberBold.setWrapText(false); // ???sheet for (long id : idList) { AccessoryDischargeInvoice invoice = arService.loadDischargeInvoice(id); if (null == invoice) { continue; } Sheet sheet = wb.createSheet("?#" + id); // ??xxxxxx Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("??"); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftId); cell.setCellValue(id); // yyyy-MM-dd HH:mm row = sheet.createRow(1); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftTime); cell.setCellValue(invoice.getTime()); // xxx row = sheet.createRow(2); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(invoice.getApplicant().getName()); // xxx row = sheet.createRow(3); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(invoice.getOperator().getName()); // row = sheet.createRow(4); row.setHeightInPoints(20); for (int i = 0; i < 7; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue(DIS_HEADERS[i]); sheet.setColumnWidth(i, DIS_HEADERS_WIDTH[i] * 256); } // List<AccessoryDischarge> discharges = invoice.getDischarges(); int rowNumber = 5; int totalQuantity = 0; for (AccessoryDischarge discharge : discharges) { Accessory acs = discharge.getAccessory(); row = sheet.createRow(rowNumber); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getType().getTitle()); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getName()); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getBrand()); cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getModel()); cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getDescription()); cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(discharge.getQuantity()); totalQuantity += discharge.getQuantity(); cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue(acs.getUnit()); rowNumber++; } // ? row = sheet.createRow(rowNumber); row.setHeightInPoints(20); cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue("?"); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(discharges.size()); cell = row.createCell(5, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalQuantity); } String filename = null; if (isIE()) { filename = URLEncoder.encode(fn.toString(), "UTF-8").replaceAll("\\+", "%20"); } else { filename = new String(fn.toString().getBytes("UTF-8"), "iso8859-1"); } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 4096); wb.write(out); wb.close(); out.close(); }
From source file:com.jeans.iservlet.controller.impl.ExportController.java
/** * ?????/* w w w .jav a 2s. c o m*/ * * @param ids * ??(AccessoryInvoiceDetail.id)? * @return * @throws IOException */ private void exportAccessoryRegistries(List<Long> idList) throws IOException { StringBuilder fn = new StringBuilder(getCurrentCompany().getName()); fn.append(" - ???.xlsx"); Workbook wb = new XSSFWorkbook(); DataFormat df = wb.createDataFormat(); // ?10?? Font bFont = wb.createFont(); bFont.setBoldweight(Font.BOLDWEIGHT_BOLD); bFont.setFontName(""); bFont.setFontHeightInPoints((short) 10); // ?10? Font nFont = wb.createFont(); nFont.setFontName(""); nFont.setFontHeightInPoints((short) 10); // ??1??????? CellStyle csLeftTextBold = wb.createCellStyle(); csLeftTextBold.setAlignment(CellStyle.ALIGN_LEFT); csLeftTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftTextBold.setFont(bFont); csLeftTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftTextBold.setWrapText(false); // ??2??????? CellStyle csLeftText = wb.createCellStyle(); csLeftText.setAlignment(CellStyle.ALIGN_LEFT); csLeftText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftText.setFont(nFont); csLeftText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csLeftText.setWrapText(false); // ??3??????? CellStyle csCenterTime = wb.createCellStyle(); csCenterTime.setAlignment(CellStyle.ALIGN_CENTER); csCenterTime.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterTime.setFont(nFont); csCenterTime.setDataFormat(df.getFormat("yyyy-MM-dd HH:mm")); csCenterTime.setWrapText(false); // ??4??????? CellStyle csCenterTextBold = wb.createCellStyle(); csCenterTextBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterTextBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterTextBold.setFont(bFont); csCenterTextBold.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterTextBold.setWrapText(false); // ??5??????? CellStyle csCenterText = wb.createCellStyle(); csCenterText.setAlignment(CellStyle.ALIGN_CENTER); csCenterText.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterText.setFont(nFont); csCenterText.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); csCenterText.setWrapText(false); // ??6??????? CellStyle csCenterNumber = wb.createCellStyle(); csCenterNumber.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumber.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumber.setFont(nFont); csCenterNumber.setDataFormat(df.getFormat("#")); csCenterNumber.setWrapText(false); // ??7????8???? CellStyle csLeftId = wb.createCellStyle(); csLeftId.setAlignment(CellStyle.ALIGN_LEFT); csLeftId.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csLeftId.setFont(nFont); csLeftId.setDataFormat(df.getFormat("#00000000")); csLeftId.setWrapText(false); // ??8????8???? CellStyle csCenterId = wb.createCellStyle(); csCenterId.setAlignment(CellStyle.ALIGN_CENTER); csCenterId.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterId.setFont(nFont); csCenterId.setDataFormat(df.getFormat("#00000000")); csCenterId.setWrapText(false); // ??9??????? CellStyle csCenterNumberBold = wb.createCellStyle(); csCenterNumberBold.setAlignment(CellStyle.ALIGN_CENTER); csCenterNumberBold.setVerticalAlignment(CellStyle.VERTICAL_CENTER); csCenterNumberBold.setFont(bFont); csCenterNumberBold.setDataFormat(df.getFormat("#")); csCenterNumberBold.setWrapText(false); // ????sheet long currAcsId = 0; Sheet sheet = null; int rowNumber = 0, totalIn = 0, totalOut = 0; for (long id : idList) { AccessoryInvoiceDetail registry = arService.loadRegistry(id); if (null == registry) { continue; } Accessory acs = registry.getAccessory(); if (acs.getId() != currAcsId) { // ???sheet?? if (null != sheet) { Row row = sheet.createRow(rowNumber); row.setHeightInPoints(20); Cell cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue("?"); cell = row.createCell(3, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalIn); cell = row.createCell(4, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumberBold); cell.setCellValue(totalOut); } // ?sheet? currAcsId = acs.getId(); sheet = wb.createSheet("???#" + currAcsId); // ??xxxxxx Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("??"); cell = row.createCell(2, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csLeftId); cell.setCellValue(currAcsId); // xxxxxx row = sheet.createRow(1); row.setHeightInPoints(20); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue(""); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(acs.getType().getTitle()); // ??xxxxxx row = sheet.createRow(2); row.setHeightInPoints(20); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("??"); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(acs.getName()); // ?xxxxxx row = sheet.createRow(3); row.setHeightInPoints(20); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("?"); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(acs.getBrand()); // ?xxxxxx row = sheet.createRow(4); row.setHeightInPoints(20); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("?"); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(acs.getModel()); // ??xxxxxx row = sheet.createRow(5); row.setHeightInPoints(20); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("???"); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(acs.getDescription()); // ???xxxxxx row = sheet.createRow(6); row.setHeightInPoints(20); cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftTextBold); cell.setCellValue("???"); cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(csLeftText); cell.setCellValue(acs.getUnit()); // row = sheet.createRow(7); row.setHeightInPoints(20); for (int i = 0; i < 6; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterTextBold); cell.setCellValue(REG_HEADERS[i]); sheet.setColumnWidth(i, REG_HEADERS_WIDTH[i] * 256); } rowNumber = 8; totalIn = 0; totalOut = 0; } // ? Row row = sheet.createRow(rowNumber); row.setHeightInPoints(20); Cell cell = row.createCell(0, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(rowNumber - 7); cell = row.createCell(1, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterTime); cell.setCellValue(registry.getInvoice().getTime()); cell = row.createCell(2, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterId); cell.setCellValue(registry.getInvoice().getId()); cell = row.createCell(3, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(id > 0 ? registry.getQuantity() : 0); cell = row.createCell(4, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(csCenterNumber); cell.setCellValue(id < 0 ? registry.getQuantity() : 0); cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(csCenterText); cell.setCellValue( id < 0 ? ((AccessoryDischargeInvoice) registry.getInvoice()).getApplicant().getName() : ""); if (id > 0) { totalIn += registry.getQuantity(); } else { totalOut += registry.getQuantity(); } rowNumber++; } String filename = null; if (isIE()) { filename = URLEncoder.encode(fn.toString(), "UTF-8").replaceAll("\\+", "%20"); } else { filename = new String(fn.toString().getBytes("UTF-8"), "iso8859-1"); } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 4096); wb.write(out); wb.close(); out.close(); }
From source file:com.jeans.iservlet.controller.impl.ExportController.java
/** * ???/*from ww w . ja v a2s . co m*/ * * @return * @throws IOException */ @RequestMapping(method = RequestMethod.POST, value = "/systems") public ResponseEntity<byte[]> exportITSystems() throws IOException { StringBuilder fn = new StringBuilder(getCurrentCompany().getName()); fn.append(" - ??("); Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("?"); // ? // // ?10?? Font tFont = sheet.getWorkbook().createFont(); tFont.setBoldweight(Font.BOLDWEIGHT_BOLD); tFont.setFontName(""); tFont.setFontHeightInPoints((short) 10); // ????????? CellStyle cellStyleTitle = sheet.getWorkbook().createCellStyle(); cellStyleTitle.setAlignment(CellStyle.ALIGN_CENTER); cellStyleTitle.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleTitle.setFont(tFont); cellStyleTitle.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cellStyleTitle.setWrapText(false); // 20 Row row = sheet.createRow(0); row.setHeightInPoints(20); Cell cell = null; for (int i = 0; i < ITSYSTEM_HEADERS.length; i++) { cell = row.createCell(i, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleTitle); cell.setCellValue(ITSYSTEM_HEADERS[i]); sheet.setColumnWidth(i, ITSYSTEM_HEADERS_WIDTH[i] * 256); } // ?????->?->???? List<ITSystem> systems = new ArrayList<ITSystem>(systService.listSystems(getCurrentCompany(), null, null)); Collections.sort(systems, new Comparator<ITSystem>() { @Override public int compare(ITSystem o1, ITSystem o2) { int ret = o1.getType().ordinal() - o2.getType().ordinal(); if (ret == 0) { ret = Long.compare(o1.getOwner().getId(), o2.getOwner().getId()); if (ret == 0) { ret = Collator.getInstance(java.util.Locale.CHINA).compare(o1.getName(), o2.getName()); } } return ret; } }); // ?? DataFormat df = sheet.getWorkbook().createDataFormat(); // ?10? Font font = sheet.getWorkbook().createFont(); font.setFontName(""); font.setFontHeightInPoints((short) 10); // ?1??????? CellStyle cellStyleString = sheet.getWorkbook().createCellStyle(); cellStyleString.setAlignment(CellStyle.ALIGN_CENTER); cellStyleString.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleString.setFont(font); cellStyleString.setDataFormat(HSSFDataFormat.getBuiltinFormat("text")); cellStyleString.setWrapText(false); // ?2????(yyyyMMdd)??? CellStyle cellStyleDate = sheet.getWorkbook().createCellStyle(); cellStyleDate.setAlignment(CellStyle.ALIGN_CENTER); cellStyleDate.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleDate.setFont(font); cellStyleDate.setDataFormat(df.getFormat("yyyyMMdd")); cellStyleDate.setWrapText(false); // ?3??????(#)??? CellStyle cellStyleQuantity = sheet.getWorkbook().createCellStyle(); cellStyleQuantity.setAlignment(CellStyle.ALIGN_CENTER); cellStyleQuantity.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleQuantity.setFont(font); cellStyleQuantity.setDataFormat(df.getFormat("0")); cellStyleQuantity.setWrapText(false); // ?4?????(#,##0.00_ )??? CellStyle cellStyleCost = sheet.getWorkbook().createCellStyle(); cellStyleCost.setAlignment(CellStyle.ALIGN_RIGHT); cellStyleCost.setVerticalAlignment(CellStyle.VERTICAL_CENTER); cellStyleCost.setFont(font); cellStyleCost.setDataFormat(df.getFormat("#,##0.00_ ")); cellStyleCost.setWrapText(false); // sheet int rowNumber = 1; for (ITSystem system : systems) { // 20 row = sheet.createRow(rowNumber); row.setHeightInPoints(20); // ? Set<SystemBranch> branches = system.getBranches(); SystemBranch localBranch = null; // ?? long localId = 0; if (getCurrentCompany().getLevel() == Company.BRANCH) { localId = getCurrentCompany().getSuperior().getId(); } else { localId = getCurrentCompany().getId(); } BigDecimal cost = new BigDecimal("0.0"); // for (SystemBranch branch : branches) { cost.add(branch.getCost()); if (branch.getCompany().getId() == localId) { localBranch = branch; } } boolean branched = (localBranch != null); // ? boolean owned = system.getOwner().getId() == getCurrentCompany().getId(); // ????? // // cell = row.createCell(0, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getType().getTitle()); // ?? cell = row.createCell(1, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getName()); // cell = row.createCell(2, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getAlias()); // /? cell = row.createCell(3, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getModelOrVersion()); // cell = row.createCell(4, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getBrief()); // ? cell = row.createCell(5, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(ITSYSTEM_sLevel[system.getSecurityLevel()]); // ??? cell = row.createCell(6, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getSecurityCode()); // ? cell = row.createCell(7, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getUsersBrief()); // cell = row.createCell(8, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(null == system.getProvider() ? "" : system.getProvider().getAlias()); // ? cell = row.createCell(9, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getOwner().getAlias()); // cell = row.createCell(10, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getScope().getTitle(system.getOwner().getLevel())); // cell = row.createCell(11, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getDeploy().getTitle()); // cell = row.createCell(12, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(branched ? "" : ""); if (branched) { // ?() cell = row.createCell(13, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleCost); cell.setCellValue(localBranch.getCost().doubleValue()); // ? cell = row.createCell(14, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(localBranch.getStage().getTitle()); // ? cell = row.createCell(15, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date ct = localBranch.getConstructedTime(); if (null != ct) { cell.setCellValue(ct); } // ? cell = row.createCell(16, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date at = localBranch.getAbandonedTime(); if (null != at) { cell.setCellValue(at); } } // ?? cell = row.createCell(17, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleQuantity); cell.setCellValue(system.getFreeMaintainMonths()); if (owned) { // cell = row.createCell(18, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleQuantity); cell.setCellValue(branches.size()); // ?) cell = row.createCell(19, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleCost); cell.setCellValue(cost.doubleValue()); // ?? cell = row.createCell(20, Cell.CELL_TYPE_STRING); cell.setCellStyle(cellStyleString); cell.setCellValue(system.getStage().getTitle()); // ? cell = row.createCell(21, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date ct = system.getConstructedTime(); if (null != ct) { cell.setCellValue(ct); } // ? cell = row.createCell(22, Cell.CELL_TYPE_NUMERIC); cell.setCellStyle(cellStyleDate); Date at = system.getAbandonedTime(); if (null != at) { cell.setCellValue(at); } } rowNumber++; } fn.append((new SimpleDateFormat("yyyyMMdd")).format(new Date())).append(").xlsx"); String filename = null; if (isIE()) { filename = URLEncoder.encode(fn.toString(), "UTF-8").replaceAll("\\+", "%20"); } else { filename = new String(fn.toString().getBytes("UTF-8"), "iso8859-1"); } response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setHeader("Content-disposition", "attachment; filename=" + filename); BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream(), 4096); wb.write(out); wb.close(); out.close(); return null; }
From source file:com.liferay.dynamic.data.lists.internal.exporter.DDLXLSExporter.java
License:Open Source License
protected CellStyle createCellStyle(Workbook workbook, boolean bold, String fontName, short heightInPoints) { Font font = workbook.createFont(); font.setBold(bold);// w w w .j av a 2 s . c om font.setFontHeightInPoints(heightInPoints); font.setFontName(fontName); CellStyle style = workbook.createCellStyle(); style.setFont(font); return style; }
From source file:com.liferay.portlet.documentlibrary.action.EditEntryAction.java
License:Open Source License
public static void exportDocumentData(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws NumberFormatException, PortalException, SystemException { long file_id = 0; String fileEntryIds = ParamUtil.getString(resourceRequest, "fileEntryIds"); String[] fileentires = fileEntryIds.split(","); List<Long> tempResults = new ArrayList<Long>(); if (!fileEntryIds.isEmpty()) { if (fileentires[0].equals("true")) { for (int i = 1; i < fileentires.length; i++) { DLFileEntry FileEntry = DLFileEntryLocalServiceUtil .getDLFileEntry(Long.parseLong(fileentires[i])); file_id = FileEntry.getFileEntryId(); tempResults.add(file_id); }/*from w w w . ja va 2s . com*/ } else { for (int i = 0; i < fileentires.length; i++) { DLFileEntry FileEntry = DLFileEntryLocalServiceUtil .getDLFileEntry(Long.parseLong(fileentires[i])); file_id = FileEntry.getFileEntryId(); tempResults.add(file_id); } } } try { HSSFWorkbook hwb = new HSSFWorkbook(); HSSFSheet sheet = hwb.createSheet("Site Information"); org.apache.poi.ss.usermodel.Font font = hwb.createFont(); font.setFontHeightInPoints((short) 11); font.setFontName("Arial"); font.setItalic(false); font.setStrikeout(false); font.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); CellStyle style = hwb.createCellStyle(); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBorderLeft(CellStyle.BORDER_THIN); style.setBorderTop(CellStyle.BORDER_THIN); style.setBorderRight(CellStyle.BORDER_THIN); style.setBorderBottom(CellStyle.BORDER_THIN); style.setBottomBorderColor(HSSFColor.BLACK.index); style.setFont(font); CellStyle filterStyle = hwb.createCellStyle(); org.apache.poi.ss.usermodel.Font filterfont = hwb.createFont(); filterfont.setFontHeightInPoints((short) 9); filterfont.setFontName("Courier New"); filterfont.setItalic(false); filterfont.setStrikeout(false); filterfont.setBoldweight(org.apache.poi.ss.usermodel.Font.BOLDWEIGHT_BOLD); filterStyle.setFont(filterfont); filterStyle.setBorderBottom(CellStyle.BORDER_THIN); filterStyle.setBorderLeft(CellStyle.BORDER_THIN); filterStyle.setBorderTop(CellStyle.BORDER_THIN); filterStyle.setBorderRight(CellStyle.BORDER_THIN); HSSFRow rowhead = sheet.createRow((short) 2); rowhead.createCell((short) 0).setCellValue("S.No."); rowhead.createCell((short) 1).setCellValue("Title "); rowhead.createCell((short) 2).setCellValue("Site Name "); rowhead.createCell((short) 3).setCellValue("Category Type"); rowhead.createCell((short) 4).setCellValue("Com"); rowhead.createCell((short) 5).setCellValue("Upload Date"); rowhead.createCell((short) 6).setCellValue("File Type"); int index = 3; int sno = 0; for (int i = 0; i < tempResults.size(); i++) { sno++; DLFileEntry objdlfileentry = DLFileEntryLocalServiceUtil.getDLFileEntry(tempResults.get(i)); docs_customData objdocs_customData = null; try { objdocs_customData = docs_customDataLocalServiceUtil.getfileEntry(tempResults.get(i)); } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } String com = ""; if (objdocs_customData != null) { com = objdocs_customData.getCom(); } String siteName = ""; if (objdocs_customData != null) { siteName = objdocs_customData.getSite(); } String doccategory = ""; if (objdocs_customData != null) { doccategory = objdocs_customData.getCategory(); } HSSFRow row = sheet.createRow((short) index); row.createCell((short) 0).setCellValue(sno); row.createCell((short) 1).setCellValue(objdlfileentry.getTitle()); row.createCell((short) 2).setCellValue(siteName); row.createCell((short) 3).setCellValue(doccategory); row.createCell((short) 4).setCellValue(com); row.createCell((short) 5).setCellValue(objdlfileentry.getModifiedDate()); row.createCell((short) 6).setCellValue(objdlfileentry.getMimeType()); index++; } resourceResponse.setContentType("application/vnd.ms-excel"); resourceResponse.addProperty("Content-Disposition", "attachment;filename=Document_Listing.xls"); OutputStream fileOut = resourceResponse.getPortletOutputStream(); hwb.write(fileOut); fileOut.close(); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:com.lufs.java.apache.poi.example.CalendarDemo.java
License:Apache License
/** * cell styles used for formatting calendar sheets */// w w w . j a v a 2 s .co m private static Map<String, CellStyle> createStyles(Workbook wb) { Map<String, CellStyle> styles = new HashMap<>(); short borderColor = IndexedColors.GREY_50_PERCENT.getIndex(); CellStyle style; Font titleFont = wb.createFont(); titleFont.setFontHeightInPoints((short) 48); titleFont.setColor(IndexedColors.DARK_BLUE.getIndex()); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFont(titleFont); styles.put("title", style); Font monthFont = wb.createFont(); monthFont.setFontHeightInPoints((short) 12); monthFont.setColor(IndexedColors.WHITE.getIndex()); monthFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.CENTER); style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFont(monthFont); styles.put("month", style); Font dayFont = wb.createFont(); dayFont.setFontHeightInPoints((short) 14); dayFont.setBold(true); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.LEFT); style.setVerticalAlignment(VerticalAlignment.TOP); style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderLeft(BorderStyle.THIN); style.setLeftBorderColor(borderColor); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(borderColor); style.setFont(dayFont); styles.put("weekend_left", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.TOP); style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(borderColor); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(borderColor); styles.put("weekend_right", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.LEFT); style.setVerticalAlignment(VerticalAlignment.TOP); style.setBorderLeft(BorderStyle.THIN); style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setLeftBorderColor(borderColor); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(borderColor); style.setFont(dayFont); styles.put("workday_left", style); style = wb.createCellStyle(); style.setAlignment(HorizontalAlignment.CENTER); style.setVerticalAlignment(VerticalAlignment.TOP); style.setFillForegroundColor(IndexedColors.WHITE.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(borderColor); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(borderColor); styles.put("workday_right", style); style = wb.createCellStyle(); style.setBorderLeft(BorderStyle.THIN); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(borderColor); styles.put("grey_left", style); style = wb.createCellStyle(); style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setBorderRight(BorderStyle.THIN); style.setRightBorderColor(borderColor); style.setBorderBottom(BorderStyle.THIN); style.setBottomBorderColor(borderColor); styles.put("grey_right", style); return styles; }
From source file:com.lw.common.utils.ExcelUtil.java
/** * excel/*from w ww .j av a 2s. c o m*/ * @param list ? * @param keys listmapkey? * @param columnNames excel?? * */ public static Workbook createWorkBook(List<Map<String, Object>> list, String[] keys, String columnNames[]) { // excel Workbook wb = new HSSFWorkbook(); // sheet?? Sheet sheet = wb.createSheet(list.get(0).get("sheetName").toString()); // ???n? for (int i = 0; i < keys.length; i++) { sheet.setColumnWidth((short) i, (short) (35.7 * 150)); } // Row row = sheet.createRow((short) 0); // ??? CellStyle cs = wb.createCellStyle(); CellStyle cs2 = wb.createCellStyle(); // ? Font f = wb.createFont(); Font f2 = wb.createFont(); // ???? f.setFontHeightInPoints((short) 10); f.setColor(IndexedColors.BLACK.getIndex()); f.setBoldweight(Font.BOLDWEIGHT_BOLD); // ?? f2.setFontHeightInPoints((short) 10); f2.setColor(IndexedColors.BLACK.getIndex()); // Font f3=wb.createFont(); // f3.setFontHeightInPoints((short) 10); // f3.setColor(IndexedColors.RED.getIndex()); // ????? cs.setFont(f); cs.setBorderLeft(CellStyle.BORDER_THIN); cs.setBorderRight(CellStyle.BORDER_THIN); cs.setBorderTop(CellStyle.BORDER_THIN); cs.setBorderBottom(CellStyle.BORDER_THIN); cs.setAlignment(CellStyle.ALIGN_CENTER); // ??? cs2.setFont(f2); cs2.setBorderLeft(CellStyle.BORDER_THIN); cs2.setBorderRight(CellStyle.BORDER_THIN); cs2.setBorderTop(CellStyle.BORDER_THIN); cs2.setBorderBottom(CellStyle.BORDER_THIN); cs2.setAlignment(CellStyle.ALIGN_CENTER); //?? for (int i = 0; i < columnNames.length; i++) { Cell cell = row.createCell(i); cell.setCellValue(columnNames[i]); cell.setCellStyle(cs); } //?? for (short i = 1; i < list.size(); i++) { // Row ,Cell , Row Cell 0 // sheet Row row1 = sheet.createRow((short) i); // row for (short j = 0; j < keys.length; j++) { Cell cell = row1.createCell(j); cell.setCellValue(list.get(i).get(keys[j]) == null ? " " : list.get(i).get(keys[j]).toString()); cell.setCellStyle(cs2); } } return wb; }
From source file:com.lwr.software.reporter.restservices.ReportExportService.java
License:Open Source License
public Response exportExcel(Report toExport, Set<ReportParameter> reportParams) { Workbook wb = new XSSFWorkbook(); Font boldFont = wb.createFont(); boldFont.setBold(true);/*from w w w.ja v a 2 s.c o m*/ CellStyle headerStyle = wb.createCellStyle(); headerStyle.setBorderBottom(BorderStyle.THIN); headerStyle.setBorderTop(BorderStyle.THIN); headerStyle.setBorderRight(BorderStyle.THIN); headerStyle.setBorderLeft(BorderStyle.THIN); headerStyle.setFillBackgroundColor(IndexedColors.BLUE.getIndex()); headerStyle.setFont(boldFont); CellStyle cellStyle = wb.createCellStyle(); cellStyle.setBorderBottom(BorderStyle.THIN); cellStyle.setBorderTop(BorderStyle.THIN); cellStyle.setBorderRight(BorderStyle.THIN); cellStyle.setBorderLeft(BorderStyle.THIN); CellStyle titleStyle = wb.createCellStyle(); titleStyle.setBorderBottom(BorderStyle.THIN); titleStyle.setBorderTop(BorderStyle.THIN); titleStyle.setBorderRight(BorderStyle.THIN); titleStyle.setBorderLeft(BorderStyle.THIN); List<RowElement> rows = toExport.getRows(); int sheetIndex = 0; for (RowElement rowElement : rows) { List<Element> elements = rowElement.getElements(); for (Element element : elements) { try { element.setParams(reportParams); element.init(); } catch (Exception e) { logger.error("Unable to init '" + element.getTitle() + "' element of report '" + toExport.getTitle() + "' Error " + e.getMessage(), e); return Response.serverError().entity("Unable to init '" + element.getTitle() + "' element of report '" + toExport.getTitle() + "' Error " + e.getMessage()).build(); } String sheetName = element.getTitle().substring(0, element.getTitle().length() > 30 ? 30 : element.getTitle().length()) + (sheetIndex++); Sheet sheet = wb.createSheet(sheetName); Row reportTitleRow = sheet.createRow(0); Cell reportTitleHeader = reportTitleRow.createCell(0); reportTitleHeader.setCellStyle(headerStyle); reportTitleHeader.setCellValue("Report Title:"); Cell reportTitleCell = reportTitleRow.createCell(1); reportTitleCell.setCellStyle(titleStyle); reportTitleCell.setCellValue(toExport.getTitle()); Row elementTitleRow = sheet.createRow(1); Cell elementTitleHeader = elementTitleRow.createCell(0); elementTitleHeader.setCellStyle(headerStyle); elementTitleHeader.setCellValue("Element Title:"); Cell elementTitleCell = elementTitleRow.createCell(1); elementTitleCell.setCellStyle(titleStyle); elementTitleCell.setCellValue(element.getTitle()); List<List<Object>> dataToExport = element.getData(); int rowIndex = 3; Row headerRow = sheet.createRow(rowIndex++); List<Object> unifiedHeaderRow = element.getHeader(); for (int i = 0; i < unifiedHeaderRow.size(); i++) { Cell headerCell = headerRow.createCell(i); String headerCellValue = unifiedHeaderRow.get(i).toString(); headerCell.setCellValue(headerCellValue); headerCell.setCellStyle(headerStyle); } for (int i = 0; i < dataToExport.size(); i++) { Row row = sheet.createRow(rowIndex++); List<Object> unifiedRow = dataToExport.get(i); int cellIndex = 0; for (Object cellValue : unifiedRow) { Cell cell = row.createCell(cellIndex); cell.setCellStyle(cellStyle); try { double val = Double.parseDouble(cellValue.toString()); cell.setCellValue(val); } catch (NumberFormatException e) { cell.setCellValue(cellValue.toString()); } cellIndex++; } } } } try { File file = new File(DashboardConstants.APPLN_TEMP_DIR + System.nanoTime()); logger.info("Export CSV temp file path is " + file.getAbsoluteFile()); wb.write(new FileOutputStream(file)); wb.close(); ResponseBuilder responseBuilder = Response.ok((Object) file); responseBuilder.header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); responseBuilder.header("Content-Transfer-Encoding", "binary"); responseBuilder.header("Content-Disposition", "attachment;filename=" + file.getName()); responseBuilder.header("Content-Length", file.length()); Response responseToSend = responseBuilder.build(); file.deleteOnExit(); return responseToSend; } catch (Exception e1) { return Response.serverError() .entity("Unable to export " + toExport.getTitle() + " report " + e1.getMessage()).build(); } }
From source file:com.mycompany.gannaraputakehomeexam.WritingToExcel.java
public void writeSongsToExcel(List<SongsList> songList) { /*/*from w w w .j a v a 2s .co m*/ Use XSSF for xlsx format and for xls use HSSF */ Workbook workbook = new XSSFWorkbook(); /* create new sheet */ Sheet songsSheet = workbook.createSheet("Gannarapu_Output"); XSSFCellStyle my_style = (XSSFCellStyle) workbook.createCellStyle(); /* Create XSSFFont object from the workbook */ XSSFFont my_font = (XSSFFont) workbook.createFont(); XSSFFont font = (XSSFFont) workbook.createFont(); /* setting cell color */ CellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex()); style.setFillPattern(CellStyle.SOLID_FOREGROUND); /* setting Header color */ CellStyle style2 = workbook.createCellStyle(); font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD); style2.setFont(font); style2.setAlignment(CellStyle.ALIGN_CENTER); // style2.setFillForegroundColor(IndexedColors.DARK_RED.getIndex()); // style2.setFillPattern(CellStyle.SOLID_FOREGROUND); // Row rowName = songsSheet.createRow(1); /* Merging the cells */ songsSheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 3)); /* Applying style to attribute name */ int nameCellIndex = 1; Cell namecell = rowName.createCell(nameCellIndex++); namecell.setCellValue("Name"); namecell.setCellStyle(style); Cell cel = rowName.createCell(nameCellIndex++); cel.setCellValue("Gannarapu, Anirudh"); /* Applying underline to Name */ my_font.setUnderline(XSSFFont.U_DOUBLE); my_style.setFont(my_font); /* Attaching the style to the cell */ CellStyle combined = workbook.createCellStyle(); combined.cloneStyleFrom(my_style); combined.cloneStyleFrom(style); cel.setCellStyle(combined); /* Applying colors to header */ Row rowMain = songsSheet.createRow(3); SheetConditionalFormatting sheetCF = songsSheet.getSheetConditionalFormatting(); ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("5"); PatternFormatting fill1 = rule1.createPatternFormatting(); fill1.setFillBackgroundColor(IndexedColors.RED.index); fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND); CellRangeAddress[] regions = { CellRangeAddress.valueOf("A4:F4") }; sheetCF.addConditionalFormatting(regions, rule1); /* setting new rule to apply alternate colors to cells having same Genre */ ConditionalFormattingRule rule2 = sheetCF.createConditionalFormattingRule("4"); PatternFormatting fill2 = rule2.createPatternFormatting(); fill2.setFillBackgroundColor(IndexedColors.LEMON_CHIFFON.index); fill2.setFillPattern(PatternFormatting.SOLID_FOREGROUND); CellRangeAddress[] regionsAction = { CellRangeAddress.valueOf("A5:F5"), CellRangeAddress.valueOf("A6:F6"), CellRangeAddress.valueOf("A7:F7"), CellRangeAddress.valueOf("A8:F8"), CellRangeAddress.valueOf("A13:F13"), CellRangeAddress.valueOf("A14:F14"), CellRangeAddress.valueOf("A15:F15"), CellRangeAddress.valueOf("A16:F16"), CellRangeAddress.valueOf("A23:F23"), CellRangeAddress.valueOf("A24:F24"), CellRangeAddress.valueOf("A25:F25"), CellRangeAddress.valueOf("A26:F26") }; /* setting new rule to apply alternate colors to cells having same Genre */ ConditionalFormattingRule rule3 = sheetCF.createConditionalFormattingRule("4"); PatternFormatting fill3 = rule3.createPatternFormatting(); fill3.setFillBackgroundColor(IndexedColors.LIGHT_GREEN.index); fill3.setFillPattern(PatternFormatting.SOLID_FOREGROUND); CellRangeAddress[] regionsAdv = { CellRangeAddress.valueOf("A9:F9"), CellRangeAddress.valueOf("A10:F10"), CellRangeAddress.valueOf("A11:F11"), CellRangeAddress.valueOf("A12:F12"), CellRangeAddress.valueOf("A17:F17"), CellRangeAddress.valueOf("A18:F18"), CellRangeAddress.valueOf("A19:F19"), CellRangeAddress.valueOf("A20:F20"), CellRangeAddress.valueOf("A21:F21"), CellRangeAddress.valueOf("A22:F22"), CellRangeAddress.valueOf("A27:F27"), CellRangeAddress.valueOf("A28:F28"), CellRangeAddress.valueOf("A29:F29") }; /* Applying above created rule formatting to cells */ sheetCF.addConditionalFormatting(regionsAction, rule2); sheetCF.addConditionalFormatting(regionsAdv, rule3); /* Setting coloumn header values */ int mainCellIndex = 0; Cell SNO = rowMain.createCell(mainCellIndex++); SNO.setCellValue("SNO"); SNO.setCellStyle(style2); Cell gen = rowMain.createCell(mainCellIndex++); gen.setCellValue("Genre"); gen.setCellStyle(style2); Cell credit = rowMain.createCell(mainCellIndex++); credit.setCellValue("Credit Score"); credit.setCellStyle(style2); Cell name = rowMain.createCell(mainCellIndex++); name.setCellValue("Album Name"); name.setCellStyle(style2); Cell art = rowMain.createCell(mainCellIndex++); art.setCellValue("Artist"); art.setCellStyle(style2); Cell release = rowMain.createCell(mainCellIndex++); release.setCellValue("Release Date"); release.setCellStyle(style2); /* populating cell values */ int rowIndex = 4; int sno = 1; for (SongsList song : songList) { if (song.getSno() != 0) { Row row = songsSheet.createRow(rowIndex++); int cellIndex = 0; /* first place in row is Sno */ row.createCell(cellIndex++).setCellValue(sno++); /* second place in row is Genre */ row.createCell(cellIndex++).setCellValue(song.getGenre()); /* third place in row is Critic score */ row.createCell(cellIndex++).setCellValue(song.getCriticscore()); /* fourth place in row is Album name */ row.createCell(cellIndex++).setCellValue(song.getAlbumname()); /* fifth place in row is Artist */ row.createCell(cellIndex++).setCellValue(song.getArtist()); /* sixth place in row is marks in date */ if (song.getReleasedate() != null) { Cell date = row.createCell(cellIndex++); DataFormat format = workbook.createDataFormat(); CellStyle dateStyle = workbook.createCellStyle(); dateStyle.setDataFormat(format.getFormat("dd-MMM-yyyy")); date.setCellStyle(dateStyle); date.setCellValue(song.getReleasedate()); /* auto-resizing columns */ songsSheet.autoSizeColumn(6); songsSheet.autoSizeColumn(5); songsSheet.autoSizeColumn(4); songsSheet.autoSizeColumn(3); songsSheet.autoSizeColumn(2); } } } /* writing this workbook to excel file. */ try { FileOutputStream fos = new FileOutputStream(FILE_PATH); workbook.write(fos); fos.close(); System.out.println(FILE_PATH + " is successfully written"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }