List of usage examples for org.apache.poi.hssf.usermodel HSSFWorkbook createCellStyle
@Override
public HSSFCellStyle createCellStyle()
From source file:com.beginner.core.utils.ObjectExcelView.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* ww w . ja va 2 s .c o m*/ protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { Date date = new Date(); String filename = DateUtil.date2Str(date, "yyyyMMddHHmmss"); HSSFSheet sheet; HSSFCell cell; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xls"); sheet = workbook.createSheet("sheet1"); List<String> titles = (List<String>) model.get("titles"); int len = titles.size(); HSSFCellStyle headerStyle = workbook.createCellStyle(); //? headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFFont headerFont = workbook.createFont(); // headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); headerFont.setFontHeightInPoints((short) 11); headerStyle.setFont(headerFont); short width = 20, height = 25 * 20; sheet.setDefaultColumnWidth(width); for (int i = 0; i < len; i++) { // String title = titles.get(i); cell = getCell(sheet, 0, i); cell.setCellStyle(headerStyle); setText(cell, title); } sheet.getRow(0).setHeight(height); HSSFCellStyle contentStyle = workbook.createCellStyle(); //? contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); List<PageData> varList = (List<PageData>) model.get("varList"); int varCount = varList.size(); for (int i = 0; i < varCount; i++) { PageData vpd = varList.get(i); for (int j = 0; j < len; j++) { String varstr = vpd.getString("var" + (j + 1)) != null ? vpd.getString("var" + (j + 1)) : ""; cell = getCell(sheet, i + 1, j); cell.setCellStyle(contentStyle); setText(cell, varstr); } } }
From source file:com.carfinance.module.common.controller.DocumentDownloadController.java
/** * ????Excel/* w w w.ja v a 2 s . c om*/ * @param model * @param request * @param response */ @RequestMapping(value = "/cashierstatement", method = RequestMethod.GET) public void cashierStatement(Model model, HttpServletRequest request, HttpServletResponse response) { String contrace_id_str = request.getParameter("contrace_id"); String contrace_no = ""; String customer_name = ""; String customer_no = ""; String daily_available_km = ""; String contrace_type = ""; VehicleContraceInfo vehicleContraceInfo = this.vehicleServiceManageService .getVehicleContraceInfoById(Long.valueOf(contrace_id_str)); if (vehicleContraceInfo != null) { contrace_no = vehicleContraceInfo.getContrace_no(); customer_name = vehicleContraceInfo.getCustomer_name(); daily_available_km = vehicleContraceInfo.getDaily_available_km() + ""; } else { PropertyContraceInfo propertyContraceInfo = this.vehicleServiceManageService .getPropertyContraceInfoById(Long.valueOf(contrace_id_str)); if (propertyContraceInfo != null) { contrace_no = propertyContraceInfo.getContrace_no(); customer_name = propertyContraceInfo.getCustomer_name(); contrace_type = "?"; } } List<VehicleContraceVehsInfo> vehicleContraceVehsInfoList = this.vehicleServiceManageService .getVehicleContraceVehsListByContraceId(Long.valueOf(contrace_id_str)); //1.ContentType response.setContentType("multipart/form-data"); //2.????(??a.pdf) response.setHeader("Content-Disposition", "attachment;fileName=" + contrace_no + ".xls"); // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); for (VehicleContraceVehsInfo v : vehicleContraceVehsInfoList) { // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet(v.getLicense_plate()); // sheet0,??poiExcel?short HSSFRow row = sheet.createRow((int) 0); // ? HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? HSSFCell cell = row.createCell((short) 0); cell.setCellValue("??"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("?"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue(""); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("?"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue(""); cell.setCellStyle(style); row = sheet.createRow(1); row.createCell((short) 0).setCellValue(customer_name); row.createCell((short) 1).setCellValue(customer_no); row.createCell((short) 2).setCellValue(contrace_type); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(""); row = sheet.createRow(2); HSSFCell cell2 = row.createCell((short) 0); cell2.setCellValue("?"); cell2.setCellStyle(style); cell2 = row.createCell((short) 1); cell2.setCellValue("??"); cell2.setCellStyle(style); cell2 = row.createCell((short) 2); cell2.setCellValue(""); cell2.setCellStyle(style); cell2 = row.createCell((short) 3); cell2.setCellValue(""); cell2.setCellStyle(style); cell2 = row.createCell((short) 4); cell2.setCellValue("??"); cell2.setCellStyle(style); row = sheet.createRow(3); row.createCell((short) 0).setCellValue(v.getKm()); row.createCell((short) 1).setCellValue(v.getOil_percent()); long over_km = (v.getReturn_km() - v.getKm()) > 0 ? (v.getReturn_km() - v.getKm()) : 0; row.createCell((short) 2).setCellValue(over_km); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(""); row = sheet.createRow(4); HSSFCell cell4 = row.createCell((short) 0); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 1); cell4.setCellValue("?"); cell4.setCellStyle(style); cell4 = row.createCell((short) 2); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 3); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 4); cell4.setCellValue(""); cell4.setCellStyle(style); row = sheet.createRow(5); row.createCell((short) 0).setCellValue(v.getReturn_km()); row.createCell((short) 1).setCellValue(v.getRevert_oil_percent()); long over_oil = (v.getOil_percent() - v.getRevert_oil_percent()) > 0 ? (v.getOil_percent() - v.getRevert_oil_percent()) : 0; row.createCell((short) 2).setCellValue(over_oil); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(v.getActually_price()); } // ? try { String path = appProps.get("contrace.over.download.path") + contrace_no + ".xls"; FileOutputStream fout = new FileOutputStream(path); wb.write(fout); fout.close(); ServletOutputStream out; //File(?download.pdf) File file = new File(path); try { FileInputStream inputStream = new FileInputStream(file); //3.response?ServletOutputStream(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[512]; while (b != -1) { b = inputStream.read(buffer); //4.?(out) out.write(buffer, 0, b); } inputStream.close(); out.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:com.cimmyt.reports.impl.ServiceReportKBioImpl.java
License:Apache License
/** * Method to put style to header and normally cell * @param objLibro//from ww w . ja va 2 s . co m * @param isHeader * @return */ private HSSFCellStyle styleCellNormally(HSSFWorkbook objLibro, boolean isHeader) { HSSFFont sourceStyle = objLibro.createFont(); sourceStyle.setFontHeightInPoints((short) 11); sourceStyle.setBoldweight((short) 11); sourceStyle.setFontName(HSSFFont.FONT_ARIAL); if (isHeader) { sourceStyle.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); } HSSFCellStyle styleCell = objLibro.createCellStyle(); styleCell.setWrapText(true); styleCell.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY); styleCell.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); styleCell.setFont(sourceStyle); return styleCell; }
From source file:com.cimmyt.reports.impl.ServiceReportKBioImpl.java
License:Apache License
/** * Method that pint cells in different style of foreground * @param objBook// w w w . ja va2 s .c om * @param foregroundColor * @return */ private HSSFCellStyle getStyleCeldSolidForeground(HSSFWorkbook objBook, short foregroundColor) { HSSFFont sourceStyle = objBook.createFont(); sourceStyle.setFontHeightInPoints((short) 11); sourceStyle.setFontName(HSSFFont.FONT_ARIAL); HSSFCellStyle stileCell = objBook.createCellStyle(); stileCell.setWrapText(true); stileCell.setAlignment(HSSFCellStyle.ALIGN_JUSTIFY); stileCell.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); stileCell.setFont(sourceStyle); stileCell.setFillForegroundColor(foregroundColor); stileCell.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); return stileCell; }
From source file:com.cms.utils.CommonUtils.java
public static HSSFCellStyle styleLeft(HSSFWorkbook workbook) { HSSFCellStyle cellStyleLeft = workbook.createCellStyle(); cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT); cellStyleLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyleLeft.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyleLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyleLeft.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyleLeft.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyleLeft.setWrapText(false);//from w w w.j a va 2s . com return cellStyleLeft; }
From source file:com.cms.utils.CommonUtils.java
public static HSSFCellStyle styleRight(HSSFWorkbook workbook) { //phai/*from w w w . j a va 2 s. c o m*/ HSSFCellStyle cellStyleRight = workbook.createCellStyle(); cellStyleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT); cellStyleRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyleRight.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyleRight.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyleRight.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyleRight.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyleRight.setWrapText(false); return cellStyleRight; }
From source file:com.cms.utils.CommonUtils.java
/** * * @param workbook/*w ww .j a va 2 s .co m*/ * @return HSSFCellStyle */ public static HSSFCellStyle styleCenter(HSSFWorkbook workbook) { //giua HSSFCellStyle cellStyleCenter = workbook.createCellStyle(); cellStyleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyleCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyleCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyleCenter.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyleCenter.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyleCenter.setWrapText(false); return cellStyleCenter; }
From source file:com.cms.utils.CommonUtils.java
/** * * @param workbook// w ww .j ava 2 s. c o m * @return HSSFCellStyle */ public static HSSFCellStyle styleCell(HSSFWorkbook workbook) { HSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN); cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN); cellStyle.setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cellStyle.setWrapText(false); return cellStyle; }
From source file:com.comcast.cats.config.ui.monitoring.reboot.UpTimeAndRebootStatusBean.java
License:Open Source License
public void postProcessXls(Object document) { logger.trace("postProcessXls start document " + document); if (document != null) { HSSFWorkbook workBook = (HSSFWorkbook) document; HSSFSheet sheet = workBook.getSheetAt(0); HSSFRow headerRow = sheet.getRow(0); for (int i = 0; i < headerRow.getPhysicalNumberOfCells(); i++) { sheet.setColumnWidth(i, 30 * 265); // width for 40 characters }/* w w w. jav a 2s .com*/ sheet.shiftRows(0, sheet.getLastRowNum(), 5); // shift rows 0 to n // by 1 to get space // for header sheet.addMergedRegion(CellRangeAddress.valueOf("A1:F3")); HSSFFont headerFont = workBook.createFont(); headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); HSSFCellStyle headerCellStyle = workBook.createCellStyle(); headerCellStyle.setFillForegroundColor(HSSFColor.LIGHT_CORNFLOWER_BLUE.index); headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); headerCellStyle.setFont(headerFont); headerCellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); headerCellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); HSSFCell headerCell = headerRow.createCell(0); headerCell.setCellStyle(headerCellStyle); headerCell.setCellValue("CATS Uptime and Reboot Status : " + (new Date())); HSSFCellStyle metaDataCellStyle = workBook.createCellStyle(); metaDataCellStyle.setFont(headerFont); HSSFRow metaDataRow = sheet.getRow(3); if (metaDataRow == null) { metaDataRow = sheet.createRow(3); } HSSFCell metaDataKey = metaDataRow.createCell(0); metaDataKey.setCellStyle(metaDataCellStyle); metaDataKey.setCellValue("CATS Instance"); HSSFCell metaDataValue = metaDataRow.createCell(1); metaDataValue.setCellStyle(metaDataCellStyle); metaDataValue.setCellValue(AuthController.getHostAddress()); HSSFCellStyle datatTableHeaderCellStyle = workBook.createCellStyle(); datatTableHeaderCellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); datatTableHeaderCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); datatTableHeaderCellStyle.setFont(headerFont); HSSFRow actualDataTableHeaderRow = sheet.getRow(5); for (int i = 0; i < actualDataTableHeaderRow.getPhysicalNumberOfCells(); i++) { HSSFCell cell = actualDataTableHeaderRow.getCell(i); if (cell != null) { String cellValue = cell.getStringCellValue(); cellValue = cellValue.replace("<br/> ", ""); // replace // any line // breaks cell.setCellValue(cellValue); cell.setCellStyle(datatTableHeaderCellStyle); } } } logger.trace("postProcessXls end"); }
From source file:com.commander4j.util.JExcel.java
License:Open Source License
public void exportToExcel(String filename, ResultSet rs) { try {/* www .ja v a2s . com*/ ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); int columnType = 0; String columnTypeName = ""; int recordNumber = 0; int passwordCol = -1; HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet(); HSSFCellStyle cellStyle_varchar = workbook.createCellStyle(); cellStyle_varchar.setAlignment(HorizontalAlignment.LEFT); HSSFCellStyle cellStyle_nvarchar = workbook.createCellStyle(); cellStyle_nvarchar.setAlignment(HorizontalAlignment.LEFT); HSSFCellStyle cellStyle_varchar2 = workbook.createCellStyle(); cellStyle_varchar2.setAlignment(HorizontalAlignment.LEFT); HSSFCellStyle cellStyle_title = workbook.createCellStyle(); cellStyle_title.setAlignment(HorizontalAlignment.CENTER); HSSFCellStyle cellStyle_char = workbook.createCellStyle(); cellStyle_char.setAlignment(HorizontalAlignment.LEFT); HSSFCellStyle cellStyle_date = workbook.createCellStyle(); cellStyle_date.setAlignment(HorizontalAlignment.CENTER); cellStyle_date.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); HSSFCellStyle cellStyle_timestamp = workbook.createCellStyle(); cellStyle_timestamp.setAlignment(HorizontalAlignment.CENTER); cellStyle_timestamp.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm")); HSSFCellStyle cellStyle_decimal = workbook.createCellStyle(); cellStyle_decimal.setAlignment(HorizontalAlignment.RIGHT); HSSFFont font_title = workbook.createFont(); font_title.setColor((short) 0xc); font_title.setBold(true); ; font_title.setItalic(true); font_title.setUnderline(HSSFFont.U_DOUBLE); cellStyle_title.setFont(font_title); HSSFCell cell; HSSFRow row; // rs.beforeFirst(); while (rs.next()) { recordNumber++; if (recordNumber == 1) { row = sheet.createRow((int) 0); for (int column = 1; column <= numberOfColumns; column++) { cell = row.createCell((int) (column - 1)); String columnName = rsmd.getColumnLabel(column); columnName = columnName.replace("_", " "); columnName = JUtility.capitalize(columnName); cell.setCellStyle(cellStyle_title); cell.setCellValue(columnName); if (columnName.equals("Password")) { passwordCol = column; } } } row = sheet.createRow((int) recordNumber); for (int column = 1; column <= numberOfColumns; column++) { columnType = rsmd.getColumnType(column); columnTypeName = rsmd.getColumnTypeName(column); cell = row.createCell((int) (column - 1)); try { switch (columnType) { case java.sql.Types.NVARCHAR: HSSFRichTextString rtf_nvarchar; if (column == passwordCol) { rtf_nvarchar = new HSSFRichTextString("*****"); } else { rtf_nvarchar = new HSSFRichTextString(rs.getString(column)); } cell.setCellStyle(cellStyle_nvarchar); cell.setCellValue(rtf_nvarchar); break; case java.sql.Types.VARCHAR: HSSFRichTextString rtf_varchar; if (column == passwordCol) { rtf_varchar = new HSSFRichTextString("*****"); } else { rtf_varchar = new HSSFRichTextString(rs.getString(column)); } cell.setCellStyle(cellStyle_varchar); cell.setCellValue(rtf_varchar); break; case java.sql.Types.CHAR: HSSFRichTextString rtf_char = new HSSFRichTextString(rs.getString(column)); cell.setCellStyle(cellStyle_char); cell.setCellValue(rtf_char); break; case java.sql.Types.DATE: try { cell.setCellValue(rs.getTimestamp(column)); cell.setCellStyle(cellStyle_date); } catch (Exception ex) { } break; case java.sql.Types.TIMESTAMP: try { cell.setCellValue(rs.getTimestamp(column)); cell.setCellStyle(cellStyle_timestamp); } catch (Exception ex) { } break; case java.sql.Types.DECIMAL: HSSFRichTextString rtf_decimal = new HSSFRichTextString( rs.getBigDecimal(column).toString()); cell.setCellStyle(cellStyle_decimal); cell.setCellValue(rtf_decimal); break; case java.sql.Types.NUMERIC: HSSFRichTextString rtf_decimaln = new HSSFRichTextString( rs.getBigDecimal(column).toString()); cell.setCellStyle(cellStyle_decimal); cell.setCellValue(rtf_decimaln); break; case java.sql.Types.BIGINT: HSSFRichTextString rtf_bigint = new HSSFRichTextString( rs.getBigDecimal(column).toString()); cell.setCellStyle(cellStyle_decimal); cell.setCellValue(rtf_bigint); break; case java.sql.Types.INTEGER: HSSFRichTextString rtf_int = new HSSFRichTextString(String.valueOf(rs.getInt(column))); cell.setCellStyle(cellStyle_decimal); cell.setCellValue(rtf_int); break; case java.sql.Types.FLOAT: HSSFRichTextString rtf_float = new HSSFRichTextString( String.valueOf(rs.getFloat(column))); cell.setCellStyle(cellStyle_decimal); cell.setCellValue(rtf_float); break; case java.sql.Types.DOUBLE: HSSFRichTextString rtf_double = new HSSFRichTextString( String.valueOf(rs.getDouble(column))); cell.setCellStyle(cellStyle_decimal); cell.setCellValue(rtf_double); break; default: cell.setCellValue(new HSSFRichTextString(columnTypeName)); break; } } catch (Exception ex) { String errormessage = ex.getLocalizedMessage(); HSSFRichTextString rtf_exception = new HSSFRichTextString(errormessage); cell.setCellStyle(cellStyle_varchar); cell.setCellValue(rtf_exception); break; } } if (recordNumber == 65535) { break; } } for (int column = 1; column <= numberOfColumns; column++) { sheet.autoSizeColumn((int) (column - 1)); } if (recordNumber > 0) { try { FileOutputStream fileOut = new FileOutputStream(filename.toLowerCase()); workbook.write(fileOut); fileOut.close(); } catch (Exception ex) { setErrorMessage(ex.getMessage()); } } try { workbook.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SQLException e) { setErrorMessage(e.getMessage()); } }