| | |
| | | import org.apache.poi.hssf.usermodel.HSSFWorkbook; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | |
| | | import java.net.URLEncoder; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | /** |
| | | * Created by vincent on 2/17/2024 |
| | | */ |
| | | @Slf4j |
| | | public class ExcelUtil { |
| | | private static final Pattern EXTEND_FIELD_SOURCE_PATTERN = Pattern.compile("^extendFields\\.\\[(.+)]$"); |
| | | |
| | | public static void build(Workbook workbook, HttpServletResponse response) { |
| | | response.reset(); |
| | | Http.cors(response); |
| | |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (value != null) { |
| | | if (value instanceof Date) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | row.createCell(cellIndex).setCellValue(sdf.format((Date) value)); |
| | | } else { |
| | | row.createCell(cellIndex).setCellValue(value.toString()); |
| | | } |
| | | } |
| | | writeCellValue(row, cellIndex, value); |
| | | cellIndex++; |
| | | } |
| | | } |
| | |
| | | return workbook; |
| | | } |
| | | |
| | | public static Workbook create(List<Map<String, Object>> rows, List<ExportColumn> columns) { |
| | | return create(rows, columns, null); |
| | | } |
| | | |
| | | public static Workbook create(List<Map<String, Object>> rows, List<ExportColumn> columns, ExportMeta exportMeta) { |
| | | XSSFWorkbook workbook = new XSSFWorkbook(); |
| | | Sheet sheet = workbook.createSheet("export"); |
| | | int titleColumnCount = Math.max(columns.size(), 4); |
| | | int currentRowIndex = 0; |
| | | |
| | | CellStyle titleStyle = createTitleStyle(workbook); |
| | | CellStyle subHeaderStyle = createSubHeaderStyle(workbook); |
| | | CellStyle headerStyle = createHeaderStyle(workbook); |
| | | CellStyle bodyStyle = createBodyStyle(workbook); |
| | | |
| | | if (exportMeta != null && StringUtils.isNotBlank(exportMeta.getReportTitle())) { |
| | | Row titleRow = sheet.createRow(currentRowIndex++); |
| | | titleRow.setHeightInPoints(24); |
| | | Cell titleCell = titleRow.createCell(0); |
| | | titleCell.setCellValue(exportMeta.getReportTitle()); |
| | | titleCell.setCellStyle(titleStyle); |
| | | sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, titleColumnCount - 1)); |
| | | |
| | | Row subHeaderRow = sheet.createRow(currentRowIndex++); |
| | | writeSubHeaderCell(subHeaderRow, 0, getSubHeaderText("报表日期", exportMeta.getReportDate()), subHeaderStyle); |
| | | writeSubHeaderCell(subHeaderRow, 1, getSubHeaderText("打印人", exportMeta.getOperator()), subHeaderStyle); |
| | | writeSubHeaderCell(subHeaderRow, 2, getSubHeaderText("打印时间", exportMeta.getPrintedAt()), subHeaderStyle); |
| | | writeSubHeaderCell(subHeaderRow, 3, getSubHeaderText("记录数", String.valueOf(exportMeta.getCount())), subHeaderStyle); |
| | | |
| | | currentRowIndex++; |
| | | } |
| | | |
| | | Row header = sheet.createRow(currentRowIndex++); |
| | | for (int index = 0; index < columns.size(); index++) { |
| | | Cell headerCell = header.createCell(index); |
| | | headerCell.setCellValue(columns.get(index).getLabel()); |
| | | headerCell.setCellStyle(headerStyle); |
| | | } |
| | | |
| | | int rowIndex = currentRowIndex; |
| | | for (Map<String, Object> rowData : rows) { |
| | | Row row = sheet.createRow(rowIndex++); |
| | | for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { |
| | | Object value = getRowValue(rowData, columns.get(columnIndex).getSource()); |
| | | writeCellValue(row, columnIndex, value, bodyStyle); |
| | | } |
| | | } |
| | | |
| | | for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) { |
| | | sheet.autoSizeColumn(columnIndex); |
| | | } |
| | | |
| | | return workbook; |
| | | } |
| | | |
| | | private static Object getRowValue(Map<String, Object> rowData, String source) { |
| | | if (rowData == null || StringUtils.isBlank(source)) { |
| | | return null; |
| | | } |
| | | |
| | | if (rowData.containsKey(source)) { |
| | | return rowData.get(source); |
| | | } |
| | | |
| | | Matcher matcher = EXTEND_FIELD_SOURCE_PATTERN.matcher(source); |
| | | if (matcher.matches()) { |
| | | Object extendFields = rowData.get("extendFields"); |
| | | if (extendFields instanceof Map<?, ?> extendFieldMap) { |
| | | return extendFieldMap.get(matcher.group(1)); |
| | | } |
| | | } |
| | | |
| | | return rowData.get(source); |
| | | } |
| | | |
| | | private static void writeCellValue(Row row, int cellIndex, Object value) { |
| | | writeCellValue(row, cellIndex, value, null); |
| | | } |
| | | |
| | | private static void writeCellValue(Row row, int cellIndex, Object value, CellStyle cellStyle) { |
| | | Cell cell = row.createCell(cellIndex); |
| | | if (cellStyle != null) { |
| | | cell.setCellStyle(cellStyle); |
| | | } |
| | | if (value == null) { |
| | | return; |
| | | } |
| | | if (value instanceof Date) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | cell.setCellValue(sdf.format((Date) value)); |
| | | return; |
| | | } |
| | | cell.setCellValue(value.toString()); |
| | | } |
| | | |
| | | private static void writeSubHeaderCell(Row row, int cellIndex, String value, CellStyle cellStyle) { |
| | | Cell cell = row.createCell(cellIndex); |
| | | cell.setCellValue(value); |
| | | cell.setCellStyle(cellStyle); |
| | | } |
| | | |
| | | private static String getSubHeaderText(String label, String value) { |
| | | return label + ": " + StringUtils.defaultString(value); |
| | | } |
| | | |
| | | private static CellStyle createTitleStyle(Workbook workbook) { |
| | | CellStyle cellStyle = workbook.createCellStyle(); |
| | | cellStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | |
| | | Font font = workbook.createFont(); |
| | | font.setBold(true); |
| | | font.setFontHeightInPoints((short) 14); |
| | | cellStyle.setFont(font); |
| | | return cellStyle; |
| | | } |
| | | |
| | | private static CellStyle createSubHeaderStyle(Workbook workbook) { |
| | | CellStyle cellStyle = workbook.createCellStyle(); |
| | | cellStyle.setAlignment(HorizontalAlignment.LEFT); |
| | | cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | |
| | | Font font = workbook.createFont(); |
| | | font.setFontHeightInPoints((short) 10); |
| | | cellStyle.setFont(font); |
| | | return cellStyle; |
| | | } |
| | | |
| | | private static CellStyle createHeaderStyle(Workbook workbook) { |
| | | CellStyle cellStyle = workbook.createCellStyle(); |
| | | cellStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | cellStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | |
| | | Font font = workbook.createFont(); |
| | | font.setBold(true); |
| | | font.setFontHeightInPoints((short) 10); |
| | | cellStyle.setFont(font); |
| | | return cellStyle; |
| | | } |
| | | |
| | | private static CellStyle createBodyStyle(Workbook workbook) { |
| | | CellStyle cellStyle = workbook.createCellStyle(); |
| | | cellStyle.setAlignment(HorizontalAlignment.LEFT); |
| | | cellStyle.setVerticalAlignment(VerticalAlignment.TOP); |
| | | cellStyle.setWrapText(true); |
| | | return cellStyle; |
| | | } |
| | | |
| | | public static class ExportColumn { |
| | | private final String source; |
| | | private final String label; |
| | | |
| | | public ExportColumn(String source, String label) { |
| | | this.source = source; |
| | | this.label = label; |
| | | } |
| | | |
| | | public String getSource() { |
| | | return source; |
| | | } |
| | | |
| | | public String getLabel() { |
| | | return label; |
| | | } |
| | | } |
| | | |
| | | public static class ExportMeta { |
| | | private final String reportTitle; |
| | | private final String reportDate; |
| | | private final String printedAt; |
| | | private final String operator; |
| | | private final int count; |
| | | |
| | | public ExportMeta(String reportTitle, String reportDate, String printedAt, String operator, int count) { |
| | | this.reportTitle = reportTitle; |
| | | this.reportDate = reportDate; |
| | | this.printedAt = printedAt; |
| | | this.operator = operator; |
| | | this.count = count; |
| | | } |
| | | |
| | | public String getReportTitle() { |
| | | return reportTitle; |
| | | } |
| | | |
| | | public String getReportDate() { |
| | | return reportDate; |
| | | } |
| | | |
| | | public String getPrintedAt() { |
| | | return printedAt; |
| | | } |
| | | |
| | | public String getOperator() { |
| | | return operator; |
| | | } |
| | | |
| | | public int getCount() { |
| | | return count; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 添加导入excel配置参数 |
| | | * 注:默认配置可满足当前需求 |