|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 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 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|