/** * 导出对象到Excel,不是基于模板的,直接新建一个Excel完成导出,基于流 * * @param os 输出流 * @param objs 对象列表 * @param clz 对象类型 * @param isXssf 是否是2007版本 */ public void exportExcelByPath( OutputStream os, List objs, Class clz, boolean isXssf, String message) { try { Workbook wb = handleExcel(objs, clz, isXssf, message); wb.write(os); } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(e); } catch (IOException e) { e.printStackTrace(); logger.error(e); } }
/** * 导出对象到Excel,直接新建一个Excel完成导出,基于路径的导出,不基于模板 * * @param outPath 导出路径 * @param objs 对象列表 * @param clz 对象类型 * @param isXssf 是否是2007版本 */ public void exportExcelByPath( String outPath, List objs, Class clz, boolean isXssf, String message) { Workbook wb = handleExcel(objs, clz, isXssf, message); FileOutputStream fos = null; try { fos = new FileOutputStream(outPath); wb.toString().getBytes("GB2312"); wb.write(fos); } catch (FileNotFoundException e) { e.printStackTrace(); logger.error(e); } catch (IOException e) { e.printStackTrace(); logger.error(e); } finally { try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); logger.error(e); } } }
private void saveExcelPoject(File file) throws IOException { Workbook wb = new XSSFWorkbook(); Sheet sheet = wb.createSheet("timeplan"); // Заголовок в 0 строке Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Филиал"); cell = row.createCell(1); cell.setCellValue("Город"); Calendar cal = Calendar.getInstance(); cal.set(2017, 0, 5); // Начальная дата проекта SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yy"); for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям cell = row.createCell(i + 2); cell.setCellValue(sdf.format(cal.getTime())); cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя } // sheet.setColumnWidth(0, 256); // Цвета ячеек CellStyle[] styles = new CellStyle[6]; styles[0] = wb.createCellStyle(); styles[0].setFillForegroundColor(HSSFColor.RED.index); styles[0].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[1] = wb.createCellStyle(); styles[1].setFillForegroundColor(HSSFColor.GREEN.index); styles[1].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[2] = wb.createCellStyle(); styles[2].setFillForegroundColor(HSSFColor.BLUE.index); styles[2].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[3] = wb.createCellStyle(); styles[3].setFillForegroundColor(HSSFColor.ROSE.index); styles[3].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[4] = wb.createCellStyle(); styles[4].setFillForegroundColor(HSSFColor.LIGHT_BLUE.index); styles[4].setFillPattern(FillPatternType.SOLID_FOREGROUND); styles[5] = wb.createCellStyle(); styles[5].setFillForegroundColor(HSSFColor.LIGHT_GREEN.index); styles[5].setFillPattern(FillPatternType.SOLID_FOREGROUND); short rowIdx = 0; for (Region region : this.regions) { row = sheet.createRow(++rowIdx); cell = row.createCell(0); cell.setCellValue(region.filial); cell = row.createCell(1); cell.setCellValue(region.name); cal = Calendar.getInstance(); cal.set(2017, 0, 5); // Начальная дата проекта for (int i = 0; i < 3 * 52; i++) { // Счетчик по неделям short color = region.getDateColorIndex(cal.getTime()); if (color >= 0) { cell = row.createCell(i + 2); cell.setCellStyle(styles[color]); } cal.add(Calendar.WEEK_OF_YEAR, 1); // Следующая неделя } } try (FileOutputStream fileOut = new FileOutputStream(file)) { wb.write(fileOut); } }