| | |
| | | |
| | | 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); |
| | | Row header = sheet.createRow(0); |
| | | CellStyle hstyle = configHeaderCellStatic(sxssfWorkbook); |
| | | for (int i = 0; i < dataList.size(); i++) { |
| | | Cell cell = header.createCell(i); |
| | | cell.setCellValue(dataList.get(i).get(Constants.ZERO)); |
| | | // 列宽设置 |
| | | sheet.setColumnWidth(i, dataList.get(i).get(Constants.ZERO).length() * 2 * 256); |
| | | // 设置列头单元格 |
| | | cell.setCellStyle(hstyle); |
| | | } |
| | | |
| | | //总行数 |
| | | Integer rowSize = dataList.get(Constants.ZERO).size(); |
| | | //总列数 |
| | | Integer columnSize = dataList.size(); |
| | | for (int i = 1; i < rowSize; i++) { |
| | | Row row = sheet.createRow(i); |
| | | for (int j = 0; j < columnSize; j++) { |
| | | Cell cell = row.createCell(j); |
| | | cell.setCellValue(dataList.get(j).get(i)); |
| | | } |
| | | } |
| | | 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) 12); |
| | | style.setFont(font); |
| | | // 设置边框 |
| | | configCellBorder(style); |
| | | return style; |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | } |