|  |  | 
 |  |  |  | 
 |  |  | import com.doumee.core.constants.ResponseStatus; | 
 |  |  | import com.doumee.core.exception.BusinessException; | 
 |  |  | import com.doumee.core.utils.Constants; | 
 |  |  | import lombok.AllArgsConstructor; | 
 |  |  | import lombok.Data; | 
 |  |  | import org.apache.poi.ss.usermodel.*; | 
 |  |  | 
 |  |  |     private static final String DEFAULT_SHEET_NAME = "Sheet1"; | 
 |  |  |  | 
 |  |  |     private Class<T> modelClass; | 
 |  |  |     private static int maxrows = 50000; | 
 |  |  |  | 
 |  |  |     private ExcelExporter(){} | 
 |  |  |  | 
 |  |  | 
 |  |  |                 // 设置列头单元格 | 
 |  |  |                 configHeaderCell(sxssfWorkbook, cell, column.columnConfig); | 
 |  |  |             } | 
 |  |  |             List<CellStyle> styleList = new ArrayList<>(); | 
 |  |  |             for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { | 
 |  |  |                 ColumnInfo column = columns.get(columnIndex); | 
 |  |  |                 styleList.add( configDataCell(sxssfWorkbook,   column.columnConfig)); | 
 |  |  |             } | 
 |  |  |             // 创建数据记录 | 
 |  |  |             for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) { | 
 |  |  |                 Row row = sheet.createRow(rowIndex + 2); | 
 |  |  | 
 |  |  |                     ColumnInfo column = columns.get(columnIndex); | 
 |  |  |                     Cell cell = row.createCell(columnIndex); | 
 |  |  |                     cell.setCellValue(getCellData(column, data.get(rowIndex))); | 
 |  |  |  | 
 |  |  |                     // 设置数据单元格 | 
 |  |  |                     configDataCell(sxssfWorkbook, cell, column.columnConfig); | 
 |  |  |  | 
 |  |  |                     cell.setCellStyle(styleList.get(columnIndex)); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |             sheet.addMergedRegion(new CellRangeAddress(data.size()+2   ,data.size()+2,0,this.getColumns().size()-1)); | 
 |  |  |             Row endRow = sheet.createRow(data.size()+2); | 
 |  |  | //            endRow.setHeight((short) 600); | 
 |  |  |             Cell c1 = endRow.createCell(0); | 
 |  |  |             c1.setCellValue(end); | 
 |  |  |             configEndCell(sxssfWorkbook,c1); | 
 |  |  |             c1.setCellStyle(  configEndCell(sxssfWorkbook)); | 
 |  |  |             sxssfWorkbook.write(os); | 
 |  |  |             os.close(); | 
 |  |  |         } catch (Exception e) { | 
 |  |  | 
 |  |  |         SXSSFWorkbook sxssfWorkbook; | 
 |  |  |         try { | 
 |  |  |             sxssfWorkbook = new SXSSFWorkbook(); | 
 |  |  |             Sheet sheet = sxssfWorkbook.createSheet(sheetName); | 
 |  |  |             // 创建列头 | 
 |  |  |             sheet.createFreezePane(0, 1); | 
 |  |  |             Row header = sheet.createRow(0); | 
 |  |  |             List<ColumnInfo> columns = this.getColumns(); | 
 |  |  |             for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { | 
 |  |  |                 ColumnInfo column = columns.get(columnIndex); | 
 |  |  |                 Cell cell = header.createCell(columnIndex); | 
 |  |  |                 cell.setCellValue(column.columnConfig.name()); | 
 |  |  |                 // 列宽设置 | 
 |  |  |                 if (column.columnConfig.width() == -1) { | 
 |  |  |                     sheet.setColumnWidth(columnIndex, column.columnConfig.name().length() * 2 * 256); | 
 |  |  |                 } else { | 
 |  |  |                     sheet.setColumnWidth(columnIndex, column.columnConfig.width() * 2 * 256); | 
 |  |  |             int totalSheet =1; | 
 |  |  |             if( data!=null && data.size()>0){ | 
 |  |  |                 totalSheet = data.size()/maxrows; | 
 |  |  |                 if(data.size()%maxrows !=0){ | 
 |  |  |                     totalSheet += 1; | 
 |  |  |                 } | 
 |  |  |                 // 设置列头单元格 | 
 |  |  |                 configHeaderCell(sxssfWorkbook, cell, column.columnConfig); | 
 |  |  |             } | 
 |  |  |             // 创建数据记录 | 
 |  |  |             for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) { | 
 |  |  |                 Row row = sheet.createRow(rowIndex + 1); | 
 |  |  |                 for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { | 
 |  |  |                     ColumnInfo column = columns.get(columnIndex); | 
 |  |  |                     Cell cell = row.createCell(columnIndex); | 
 |  |  |                     cell.setCellValue(getCellData(column, data.get(rowIndex))); | 
 |  |  |  | 
 |  |  |                     // 设置数据单元格 | 
 |  |  |                     configDataCell(sxssfWorkbook, cell, column.columnConfig); | 
 |  |  |  | 
 |  |  |             for (int i = 0; i < totalSheet; i++) { | 
 |  |  |                 List<T> list = null; | 
 |  |  |                 if(data.size() < maxrows * (i+1)) { | 
 |  |  |                     list = data.subList(maxrows*i,data.size()); | 
 |  |  |                 }else{ | 
 |  |  |                     list = data.subList(maxrows*i,maxrows*(i+1)); | 
 |  |  |                 } | 
 |  |  |                 createSheetDataBiz(list,"【"+(i+1)+"】"+sheetName,sxssfWorkbook); | 
 |  |  |             } | 
 |  |  |             sxssfWorkbook.write(os); | 
 |  |  |             os.close(); | 
 |  |  | 
 |  |  |                 } catch (IOException e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void createSheetDataBiz(List<T> data, String sheetName, SXSSFWorkbook sxssfWorkbook) throws Exception{ | 
 |  |  |         Sheet sheet = sxssfWorkbook.createSheet(sheetName); | 
 |  |  |         // 创建列头 | 
 |  |  |         sheet.createFreezePane(0, 1); | 
 |  |  |         Row header = sheet.createRow(0); | 
 |  |  |         List<ColumnInfo> columns = this.getColumns(); | 
 |  |  |         for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { | 
 |  |  |             ColumnInfo column = columns.get(columnIndex); | 
 |  |  |             Cell cell = header.createCell(columnIndex); | 
 |  |  |             cell.setCellValue(column.columnConfig.name()); | 
 |  |  |             // 列宽设置 | 
 |  |  |             if (column.columnConfig.width() == -1) { | 
 |  |  |                 sheet.setColumnWidth(columnIndex, column.columnConfig.name().length() * 2 * 256); | 
 |  |  |             } else { | 
 |  |  |                 sheet.setColumnWidth(columnIndex, column.columnConfig.width() * 2 * 256); | 
 |  |  |             } | 
 |  |  |             // 设置列头单元格 | 
 |  |  |             configHeaderCell(sxssfWorkbook, cell, column.columnConfig); | 
 |  |  |         } | 
 |  |  |         List<CellStyle> styleList = new ArrayList<>(); | 
 |  |  |         for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { | 
 |  |  |             ColumnInfo column = columns.get(columnIndex); | 
 |  |  |             styleList.add( configDataCell(sxssfWorkbook,   column.columnConfig)); | 
 |  |  |         } | 
 |  |  |         // 创建数据记录 | 
 |  |  |         for (int rowIndex = 0; rowIndex < data.size(); rowIndex++) { | 
 |  |  |             Row row = sheet.createRow(rowIndex + 1); | 
 |  |  |             for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { | 
 |  |  |                 ColumnInfo column = columns.get(columnIndex); | 
 |  |  |                 Cell cell = row.createCell(columnIndex); | 
 |  |  |                 cell.setCellValue(getCellData(column, data.get(rowIndex))); | 
 |  |  |                 // 设置数据单元格样式 | 
 |  |  |                 cell.setCellStyle(styleList.get(columnIndex)); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | 
 |  |  |     /** | 
 |  |  |      * 配置数据单元格 | 
 |  |  |      */ | 
 |  |  |     private void configDataCell (SXSSFWorkbook workbook, Cell cell, ExcelColumn columnConfig) { | 
 |  |  |         CellStyle style = workbook.createCellStyle(); | 
 |  |  |         style.setAlignment(columnConfig.align()); | 
 |  |  |         style.setVerticalAlignment(VerticalAlignment.CENTER); | 
 |  |  |         // 设置背景 | 
 |  |  |         style.setFillPattern(FillPatternType.SOLID_FOREGROUND); | 
 |  |  |         style.setFillForegroundColor(columnConfig.dataBackgroundColor().getIndex()); | 
 |  |  |         // 字体 | 
 |  |  |         Font font = workbook.createFont(); | 
 |  |  |         font.setFontHeightInPoints(columnConfig.fontSize()); | 
 |  |  |         // 字体颜色 | 
 |  |  |         font.setColor(columnConfig.color().getIndex()); | 
 |  |  |         // 粗体 | 
 |  |  |         font.setBold(columnConfig.bold()); | 
 |  |  |         // 斜体 | 
 |  |  |         font.setItalic(columnConfig.italic()); | 
 |  |  |         style.setFont(font); | 
 |  |  |         // 边框 | 
 |  |  |         configCellBorder(style); | 
 |  |  |         style.setWrapText(true); | 
 |  |  |         cell.setCellStyle(style); | 
 |  |  |     private CellStyle configDataCell (SXSSFWorkbook workbook, ExcelColumn columnConfig) { | 
 |  |  |             CellStyle  configDataCellStyle = workbook.createCellStyle(); | 
 |  |  |             configDataCellStyle.setAlignment(columnConfig.align()); | 
 |  |  |             configDataCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); | 
 |  |  |             // 设置背景 | 
 |  |  |             configDataCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | 
 |  |  |             configDataCellStyle.setFillForegroundColor(columnConfig.dataBackgroundColor().getIndex()); | 
 |  |  |             // 字体 | 
 |  |  |             Font font = workbook.createFont(); | 
 |  |  |             font.setFontHeightInPoints(columnConfig.fontSize()); | 
 |  |  |             // 字体颜色 | 
 |  |  |             font.setColor(columnConfig.color().getIndex()); | 
 |  |  |             // 粗体 | 
 |  |  |             font.setBold(columnConfig.bold()); | 
 |  |  |             // 斜体 | 
 |  |  |             font.setItalic(columnConfig.italic()); | 
 |  |  |             configDataCellStyle.setFont(font); | 
 |  |  |             // 边框 | 
 |  |  |             configCellBorder(configDataCellStyle); | 
 |  |  |             configDataCellStyle.setWrapText(true); | 
 |  |  |  | 
 |  |  |         return configDataCellStyle; | 
 |  |  | //        cell.setCellStyle(configDataCellStyle); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  | 
 |  |  |         configCellBorder(style); | 
 |  |  |         cell.setCellStyle(style); | 
 |  |  |     } | 
 |  |  | //    public static CellStyle configEndCellStyle =null; | 
 |  |  |     /** | 
 |  |  |      * 配置列头单元格 | 
 |  |  |      */ | 
 |  |  |     private void configEndCell (SXSSFWorkbook workbook, Cell cell ) { | 
 |  |  |     private CellStyle configEndCell (SXSSFWorkbook workbook ) { | 
 |  |  |         CellStyle style = workbook.createCellStyle(); | 
 |  |  |         style.setAlignment(HorizontalAlignment.RIGHT); | 
 |  |  |         style.setVerticalAlignment(VerticalAlignment.CENTER); | 
 |  |  | 
 |  |  |         style.setFont(font); | 
 |  |  |         // 设置边框 | 
 |  |  |         configCellBorder(style); | 
 |  |  |         cell.setCellStyle(style); | 
 |  |  |         return style; | 
 |  |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 配置单元格边框 | 
 |  |  |      */ | 
 |  |  |     private void configCellBorder (CellStyle style) { | 
 |  |  |     private static void configCellBorder(CellStyle style) { | 
 |  |  |         style.setBorderTop(BorderStyle.THIN); | 
 |  |  |         style.setBorderRight(BorderStyle.THIN); | 
 |  |  |         style.setBorderBottom(BorderStyle.THIN); | 
 |  |  | 
 |  |  |         private Field field; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 导出到指定输出流 | 
 |  |  |      * @param os 输出流 | 
 |  |  |      */ | 
 |  |  |     public static void exportList (List<List<String>> dataList ,String sheetName, OutputStream os) { | 
 |  |  |         SXSSFWorkbook sxssfWorkbook; | 
 |  |  |         try { | 
 |  |  |             sxssfWorkbook = new SXSSFWorkbook(); | 
 |  |  |             Sheet sheet = sxssfWorkbook.createSheet(sheetName); | 
 |  |  |             // 创建列头 | 
 |  |  |             sheet.createFreezePane(0, 1); | 
 |  |  |             CellStyle hstyle = configHeaderCellStatic(sxssfWorkbook); | 
 |  |  |             CellStyle rowStyle = configDataCellStatic(sxssfWorkbook); | 
 |  |  |             for (int i = 0; i < dataList.size(); i++) { | 
 |  |  |                 Row row = sheet.createRow(i); | 
 |  |  |                 List<String> rowList = dataList.get(i); | 
 |  |  |                 for (int j = 0; j < rowList.size(); j++) { | 
 |  |  |                     Cell cell = row.createCell(j); | 
 |  |  |                     cell.setCellValue(rowList.get(j)); | 
 |  |  | //                    if(i==0&&j==0){ | 
 |  |  | //                        cell.setCellValue("数据日期"); | 
 |  |  | //                    }else{ | 
 |  |  | //                        cell.setCellValue(rowList.get(j)); | 
 |  |  | //                    } | 
 |  |  |                     if(i==0){ | 
 |  |  |                         sheet.setColumnWidth(i, (rowList.get(j).length() + 2 )  * 2 * 256); | 
 |  |  | //                        cell.setCellStyle(hstyle); | 
 |  |  |                     } | 
 |  |  |                     if(i==0 || j==0){ | 
 |  |  |                         cell.setCellStyle(hstyle); | 
 |  |  |                     }else{ | 
 |  |  |                         cell.setCellStyle(rowStyle); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             sxssfWorkbook.write(os); | 
 |  |  |             os.close(); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             throw new BusinessException(ResponseStatus.EXPORT_EXCEL_ERROR, e); | 
 |  |  |         } finally { | 
 |  |  |             if (os != null) { | 
 |  |  |                 try { | 
 |  |  |                     os.close(); | 
 |  |  |                 } catch (IOException e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     private static CellStyle configHeaderCellStatic (SXSSFWorkbook workbook)  { | 
 |  |  |         CellStyle style = workbook.createCellStyle(); | 
 |  |  |         style.setAlignment(HorizontalAlignment.CENTER); | 
 |  |  |         style.setVerticalAlignment(VerticalAlignment.CENTER); | 
 |  |  |         // 设置背景 | 
 |  |  |         style.setFillPattern(FillPatternType.SOLID_FOREGROUND); | 
 |  |  |         style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); | 
 |  |  |         // 字体 | 
 |  |  |         Font font = workbook.createFont(); | 
 |  |  |         font.setFontHeightInPoints((short) 10); | 
 |  |  |         style.setFont(font); | 
 |  |  |         // 设置边框 | 
 |  |  |         configCellBorder(style); | 
 |  |  |         return style; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 配置数据单元格 | 
 |  |  |      */ | 
 |  |  |     private static CellStyle configDataCellStatic  (SXSSFWorkbook workbook ) { | 
 |  |  |         CellStyle  configDataCellStyle = workbook.createCellStyle(); | 
 |  |  |         configDataCellStyle.setAlignment(HorizontalAlignment.CENTER); | 
 |  |  |         configDataCellStyle.setVerticalAlignment(VerticalAlignment.CENTER); | 
 |  |  |         // 设置背景 | 
 |  |  | //        configDataCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); | 
 |  |  | //        configDataCellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex()); | 
 |  |  |         // 字体 | 
 |  |  |         Font font = workbook.createFont(); | 
 |  |  |         font.setFontHeightInPoints((short) 10); | 
 |  |  |  | 
 |  |  |         configDataCellStyle.setFont(font); | 
 |  |  |         // 边框 | 
 |  |  |         configCellBorder(configDataCellStyle); | 
 |  |  |         configDataCellStyle.setWrapText(true); | 
 |  |  |         return configDataCellStyle; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |