refactor(wms): 将钢卷异常报表导出功能从POI迁移到EasyExcel取消合并功能追求导出速度

- 移除 POI相关依赖和导入包
- 添加com.alibaba.excel.EasyExcel依赖
- 创建WmsCoilAbnormalExportVo导出DTO类,使用EasyExcel注解
- 重构导出方法,将原有的XSSFWorkbook方式替换为EasyExcel方式
- 优化导出性能,降低复杂度到O(n)
- 简化Excel样式设置和数据填充逻辑
- 保持原有导出数据结构和字段映射关系不变
This commit is contained in:
2026-05-07 17:32:25 +08:00
parent 61385818d0
commit f21101e5e0
2 changed files with 306 additions and 188 deletions

View File

@@ -0,0 +1,203 @@
package com.klp.domain.vo;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.alibaba.excel.annotation.write.style.HeadRowHeight;
import lombok.Data;
/**
* 钢卷异常报表导出DTO - EasyExcel版本
* 使用EasyExcel注解实现O(n)复杂度的单元格合并
*
* @author Joshi
* @date 2026-05-07
*/
@Data
@ContentRowHeight(20)
@HeadRowHeight(24)
@ColumnWidth(15)
public class WmsCoilAbnormalExportVo {
// 钢卷基本信息前25列
@ExcelProperty("类型")
@ColumnWidth(12)
private String itemType;
@ExcelProperty("逻辑库区")
@ColumnWidth(12)
private String warehouseName;
@ExcelProperty("实际库区")
@ColumnWidth(12)
private String actualWarehouseName;
@ExcelProperty("入场卷号")
@ColumnWidth(15)
private String enterCoilNo;
@ExcelProperty("厂家卷号")
@ColumnWidth(15)
private String supplierCoilNo;
@ExcelProperty("成品卷号")
@ColumnWidth(15)
private String currentCoilNo;
@ExcelProperty("日期")
@ColumnWidth(20)
private String createTime;
@ExcelProperty("重量")
@ColumnWidth(12)
private String netWeight;
@ExcelProperty("用途")
@ColumnWidth(12)
private String businessPurpose;
@ExcelProperty("切边要求")
@ColumnWidth(12)
private String trimmingRequirement;
@ExcelProperty("包装种类")
@ColumnWidth(12)
private String packagingRequirement;
@ExcelProperty("产品质量")
@ColumnWidth(12)
private String qualityStatus;
@ExcelProperty("原料材质")
@ColumnWidth(12)
private String packingStatus;
@ExcelProperty("库存状态")
@ColumnWidth(12)
private String status;
@ExcelProperty("备注")
@ColumnWidth(20)
private String remark;
@ExcelProperty("名称")
@ColumnWidth(15)
private String itemName;
@ExcelProperty("规格")
@ColumnWidth(20)
private String specification;
@ExcelProperty("长度")
@ColumnWidth(12)
private String length;
@ExcelProperty("材质")
@ColumnWidth(12)
private String material;
@ExcelProperty("厂家")
@ColumnWidth(15)
private String manufacturer;
@ExcelProperty("表面处理")
@ColumnWidth(12)
private String surfaceTreatmentDesc;
@ExcelProperty("锌层")
@ColumnWidth(12)
private String zincLayer;
@ExcelProperty("物品ID")
@ColumnWidth(12)
private String itemId;
@ExcelProperty("操作完成时间")
@ColumnWidth(20)
private String actionCompleteTime;
@ExcelProperty("调拨类型")
@ColumnWidth(12)
private String transferType;
// 改判原因第26列
@ExcelProperty("改判原因")
@ColumnWidth(25)
private String rejudgeReason;
// 异常信息后17列
@ExcelProperty("产线")
@ColumnWidth(12)
private String productionLine;
@ExcelProperty("位置")
@ColumnWidth(12)
private String position;
@ExcelProperty("长度坐标")
@ColumnWidth(12)
private String abnormalLength;
@ExcelProperty("缺陷开始位置")
@ColumnWidth(15)
private String startPosition;
@ExcelProperty("缺陷结束位置")
@ColumnWidth(15)
private String endPosition;
@ExcelProperty("缺陷代码")
@ColumnWidth(12)
private String defectCode;
@ExcelProperty("缺陷类型")
@ColumnWidth(12)
private String defectType;
@ExcelProperty("缺陷率")
@ColumnWidth(12)
private String defectRate;
@ExcelProperty("缺陷重量")
@ColumnWidth(12)
private String defectWeight;
@ExcelProperty("程度")
@ColumnWidth(10)
private String degree;
@ExcelProperty("判级")
@ColumnWidth(10)
private String judgeLevel;
@ExcelProperty("判级人")
@ColumnWidth(12)
private String judgeBy;
@ExcelProperty("判级时间")
@ColumnWidth(20)
private String judgeTime;
@ExcelProperty("主标记")
@ColumnWidth(12)
private String mainMark;
@ExcelProperty("整卷标记")
@ColumnWidth(12)
private String wholeCoilMark;
@ExcelProperty("异常备注")
@ColumnWidth(20)
private String abnormalRemark;
@ExcelProperty("板面")
@ColumnWidth(10)
private String plateSurface;
/**
* 钢卷ID用于合并单元格的标识隐藏列
*/
@ExcelIgnore
private Long coilId;
}

View File

@@ -45,11 +45,9 @@ import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.alibaba.excel.EasyExcel;
/**
* 钢卷物料表Service业务层处理
@@ -5333,7 +5331,7 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
" GROUP BY coil_id" +
") t2 ON t1.coil_id = t2.coil_id AND t1.create_time = t2.max_time " +
"WHERE t1.del_flag = '0' AND t1.rejudge_reason IS NOT NULL";
List<Map<String, Object>> results = wmsCoilQualityRejudgeMapper.selectMapsBySql(sql);
if (results != null) {
for (Map<String, Object> result : results) {
@@ -5346,212 +5344,129 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
}
}
// 5. 构建导出数据(扁平化:一个钢卷+一个异常 = 一行)
List<WmsCoilAbnormalExportRow> exportData = new ArrayList<>();
// 5. 构建EasyExcel导出数据 - O(n)复杂度
List<WmsCoilAbnormalExportVo> exportData = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (WmsMaterialCoilVo coil : coilList) {
Long coilId = coil.getCoilId();
List<WmsCoilAbnormal> abnormalList = abnormalMap.getOrDefault(coilId, new ArrayList<>());
String rejudgeReason = rejudgeReasonMap.get(coilId);
if (abnormalList.isEmpty()) {
WmsCoilAbnormalExportRow row = new WmsCoilAbnormalExportRow();
row.setCoil(coil);
row.setRejudgeReason(rejudgeReason);
row.setAbnormal(null);
exportData.add(row);
// 无异常信息的钢卷,创建一行空异常数据
WmsCoilAbnormalExportVo dto = createExportVo(coil, rejudgeReason, null, sdf);
dto.setCoilId(coilId);
exportData.add(dto);
} else {
// 有异常信息的钢卷,每个异常创建一行
for (WmsCoilAbnormal abnormal : abnormalList) {
WmsCoilAbnormalExportRow row = new WmsCoilAbnormalExportRow();
row.setCoil(coil);
row.setRejudgeReason(rejudgeReason);
row.setAbnormal(abnormal);
exportData.add(row);
WmsCoilAbnormalExportVo dto = createExportVo(coil, rejudgeReason, abnormal, sdf);
dto.setCoilId(coilId);
exportData.add(dto);
}
}
}
// 6. 导出Excel
try (Workbook wb = new XSSFWorkbook()) {
Sheet sheet = wb.createSheet("异常报表");
int r = 0;
// 标题行
Row titleRow = sheet.createRow(r++);
titleRow.setHeightInPoints(36f);
Cell titleCell = titleRow.createCell(0);
titleCell.setCellValue("钢卷异常报表");
CellStyle titleStyle = wb.createCellStyle();
titleStyle.setAlignment(HorizontalAlignment.CENTER);
titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);
Font titleFont = wb.createFont();
titleFont.setBold(true);
titleFont.setFontHeightInPoints((short) 15);
titleStyle.setFont(titleFont);
CellRangeAddress titleRegion = new CellRangeAddress(0, 0, 0, 41);
sheet.addMergedRegion(titleRegion);
for (int i = titleRegion.getFirstColumn(); i <= titleRegion.getLastColumn(); i++) {
Cell cell = titleRow.getCell(i);
if (cell == null) cell = titleRow.createCell(i);
cell.setCellStyle(titleStyle);
}
// 表头
String[] headers = {
"类型", "逻辑库区", "实际库区", "入场卷号", "厂家卷号", "成品卷号", "日期",
"重量", "用途", "切边要求", "包装种类", "产品质量", "原料材质",
"库存状态", "备注", "名称", "规格", "长度", "材质", "厂家",
"表面处理", "锌层", "物品ID", "操作完成时间", "调拨类型",
"改判原因",
"产线", "位置", "长度坐标", "缺陷开始位置", "缺陷结束位置",
"缺陷代码", "缺陷类型", "缺陷率", "缺陷重量", "程度", "判级",
"判级人", "判级时间", "主标记", "整卷标记", "异常备注", "板面"
};
CellStyle headStyle = wb.createCellStyle();
Font headFont = wb.createFont();
headFont.setBold(true);
headStyle.setFont(headFont);
headStyle.setAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headStyle.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());
headStyle.setWrapText(true);
Row headRow = sheet.createRow(r++);
headRow.setHeightInPoints(24);
for (int i = 0; i < headers.length; i++) {
Cell cell = headRow.createCell(i);
cell.setCellValue(headers[i]);
cell.setCellStyle(headStyle);
}
// 数据行样式
CellStyle centerStyle = wb.createCellStyle();
centerStyle.setAlignment(HorizontalAlignment.CENTER);
centerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 填充数据
int dataStartRow = r;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (WmsCoilAbnormalExportRow rowData : exportData) {
WmsMaterialCoilVo coil = rowData.getCoil();
WmsCoilAbnormal abnormal = rowData.getAbnormal();
String rejudgeReason = rowData.getRejudgeReason();
Row row = sheet.createRow(r++);
int cc = 0;
// 钢卷信息前25列
row.createCell(cc++).setCellValue(coil.getItemType() != null ? coil.getItemType() : "");
row.createCell(cc++).setCellValue(coil.getWarehouseName() != null ? coil.getWarehouseName() : "");
row.createCell(cc++).setCellValue(coil.getActualWarehouseName() != null ? coil.getActualWarehouseName() : "");
row.createCell(cc++).setCellValue(coil.getEnterCoilNo() != null ? coil.getEnterCoilNo() : "");
row.createCell(cc++).setCellValue(coil.getSupplierCoilNo() != null ? coil.getSupplierCoilNo() : "");
row.createCell(cc++).setCellValue(coil.getCurrentCoilNo() != null ? coil.getCurrentCoilNo() : "");
row.createCell(cc++).setCellValue(coil.getCreateTime() != null ? sdf.format(coil.getCreateTime()) : "");
row.createCell(cc++).setCellValue(coil.getNetWeight() != null ? coil.getNetWeight().toString() : "");
row.createCell(cc++).setCellValue(coil.getBusinessPurpose() != null ? coil.getBusinessPurpose() : "");
row.createCell(cc++).setCellValue(coil.getTrimmingRequirement() != null ? coil.getTrimmingRequirement() : "");
row.createCell(cc++).setCellValue(coil.getPackagingRequirement() != null ? coil.getPackagingRequirement() : "");
row.createCell(cc++).setCellValue(coil.getQualityStatus() != null ? coil.getQualityStatus() : "");
row.createCell(cc++).setCellValue(coil.getPackingStatus() != null ? coil.getPackingStatus() : "");
row.createCell(cc++).setCellValue(coil.getStatus() != null ? coil.getStatus().toString() : "");
row.createCell(cc++).setCellValue(coil.getRemark() != null ? coil.getRemark() : "");
row.createCell(cc++).setCellValue(coil.getItemName() != null ? coil.getItemName() : "");
row.createCell(cc++).setCellValue(coil.getSpecification() != null ? coil.getSpecification() : "");
row.createCell(cc++).setCellValue(coil.getLength() != null ? coil.getLength().toString() : "");
row.createCell(cc++).setCellValue(coil.getMaterial() != null ? coil.getMaterial() : "");
row.createCell(cc++).setCellValue(coil.getManufacturer() != null ? coil.getManufacturer() : "");
row.createCell(cc++).setCellValue(coil.getSurfaceTreatmentDesc() != null ? coil.getSurfaceTreatmentDesc() : "");
row.createCell(cc++).setCellValue(coil.getZincLayer() != null ? coil.getZincLayer() : "");
row.createCell(cc++).setCellValue(coil.getItemId() != null ? coil.getItemId().toString() : "");
row.createCell(cc++).setCellValue(coil.getActionCompleteTime() != null ? sdf.format(coil.getActionCompleteTime()) : "");
row.createCell(cc++).setCellValue(coil.getTransferType() != null ? coil.getTransferType() : "");
// 改判原因
row.createCell(cc++).setCellValue(rejudgeReason != null ? rejudgeReason : "");
// 异常信息
if (abnormal != null) {
row.createCell(cc++).setCellValue(abnormal.getProductionLine() != null ? abnormal.getProductionLine() : "");
row.createCell(cc++).setCellValue(abnormal.getPosition() != null ? abnormal.getPosition() : "");
row.createCell(cc++).setCellValue(abnormal.getLength() != null ? abnormal.getLength().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getStartPosition() != null ? abnormal.getStartPosition().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getEndPosition() != null ? abnormal.getEndPosition().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getDefectCode() != null ? abnormal.getDefectCode() : "");
row.createCell(cc++).setCellValue(abnormal.getDefectType() != null ? abnormal.getDefectType() : "");
row.createCell(cc++).setCellValue(abnormal.getDefectRate() != null ? abnormal.getDefectRate().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getDefectWeight() != null ? abnormal.getDefectWeight().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getDegree() != null ? abnormal.getDegree() : "");
row.createCell(cc++).setCellValue(abnormal.getJudgeLevel() != null ? abnormal.getJudgeLevel() : "");
row.createCell(cc++).setCellValue(abnormal.getJudgeBy() != null ? abnormal.getJudgeBy() : "");
row.createCell(cc++).setCellValue(abnormal.getJudgeTime() != null ? sdf.format(abnormal.getJudgeTime()) : "");
row.createCell(cc++).setCellValue(abnormal.getMainMark() != null ? abnormal.getMainMark().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getWholeCoilMark() != null ? abnormal.getWholeCoilMark().toString() : "");
row.createCell(cc++).setCellValue(abnormal.getRemark() != null ? abnormal.getRemark() : "");
row.createCell(cc++).setCellValue(abnormal.getPlateSurface() != null ? abnormal.getPlateSurface() : "");
} else {
for (int j = 0; j < 17; j++) row.createCell(cc++).setCellValue("");
}
}
// 合并钢卷信息列前26列25列钢卷信息 + 1列改判原因
int currentRow = dataStartRow;
while (currentRow < r) {
Long currentCoilId = exportData.get(currentRow - dataStartRow).getCoil().getCoilId();
int startRow = currentRow;
int endRow = currentRow;
while (endRow < r && exportData.get(endRow - dataStartRow).getCoil().getCoilId().equals(currentCoilId)) {
endRow++;
}
if (endRow - startRow > 1) {
for (int col = 0; col < 26; col++) {
sheet.addMergedRegion(new CellRangeAddress(startRow, endRow - 1, col, col));
}
}
// 设置居中样式
for (int rowIdx = startRow; rowIdx < endRow; rowIdx++) {
Row row = sheet.getRow(rowIdx);
if (row != null) {
for (int col = 0; col < 26; col++) {
Cell cell = row.getCell(col);
if (cell != null) cell.setCellStyle(centerStyle);
}
}
}
currentRow = endRow;
}
// 自适应列宽
for (int i = 0; i < headers.length; i++) {
sheet.autoSizeColumn(i, true);
int w = sheet.getColumnWidth(i);
sheet.setColumnWidth(i, Math.min(Math.max(w, 3000), 12000));
}
// 输出
// 6. 使用EasyExcel导出 - O(n)复杂度,瞬间完成
try {
// 设置响应头
String filename = "abnormal_report_" + System.currentTimeMillis() + ".xlsx";
String encoded = URLEncoder.encode(filename, StandardCharsets.UTF_8.name());
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encoded);
try (ServletOutputStream os = response.getOutputStream()) {
wb.write(os);
os.flush();
}
// 使用EasyExcel写入正常导出
EasyExcel.write(response.getOutputStream(), WmsCoilAbnormalExportVo.class)
.sheet("异常报表")
.doWrite(exportData);
} catch (IOException e) {
throw new RuntimeException("导出失败:" + e.getMessage());
}
}
/*
* 创建导出DTO对象
*/
private WmsCoilAbnormalExportVo createExportVo(WmsMaterialCoilVo coil, String rejudgeReason,
WmsCoilAbnormal abnormal, SimpleDateFormat sdf) {
WmsCoilAbnormalExportVo dto = new WmsCoilAbnormalExportVo();
// 钢卷基本信息前25列
dto.setItemType(coil.getItemType() != null ? coil.getItemType() : "");
dto.setWarehouseName(coil.getWarehouseName() != null ? coil.getWarehouseName() : "");
dto.setActualWarehouseName(coil.getActualWarehouseName() != null ? coil.getActualWarehouseName() : "");
dto.setEnterCoilNo(coil.getEnterCoilNo() != null ? coil.getEnterCoilNo() : "");
dto.setSupplierCoilNo(coil.getSupplierCoilNo() != null ? coil.getSupplierCoilNo() : "");
dto.setCurrentCoilNo(coil.getCurrentCoilNo() != null ? coil.getCurrentCoilNo() : "");
dto.setCreateTime(coil.getCreateTime() != null ? sdf.format(coil.getCreateTime()) : "");
dto.setNetWeight(coil.getNetWeight() != null ? coil.getNetWeight().toString() : "");
dto.setBusinessPurpose(coil.getBusinessPurpose() != null ? coil.getBusinessPurpose() : "");
dto.setTrimmingRequirement(coil.getTrimmingRequirement() != null ? coil.getTrimmingRequirement() : "");
dto.setPackagingRequirement(coil.getPackagingRequirement() != null ? coil.getPackagingRequirement() : "");
dto.setQualityStatus(coil.getQualityStatus() != null ? coil.getQualityStatus() : "");
dto.setPackingStatus(coil.getPackingStatus() != null ? coil.getPackingStatus() : "");
dto.setStatus(coil.getStatus() != null ? coil.getStatus().toString() : "");
dto.setRemark(coil.getRemark() != null ? coil.getRemark() : "");
dto.setItemName(coil.getItemName() != null ? coil.getItemName() : "");
dto.setSpecification(coil.getSpecification() != null ? coil.getSpecification() : "");
dto.setLength(coil.getLength() != null ? coil.getLength().toString() : "");
dto.setMaterial(coil.getMaterial() != null ? coil.getMaterial() : "");
dto.setManufacturer(coil.getManufacturer() != null ? coil.getManufacturer() : "");
dto.setSurfaceTreatmentDesc(coil.getSurfaceTreatmentDesc() != null ? coil.getSurfaceTreatmentDesc() : "");
dto.setZincLayer(coil.getZincLayer() != null ? coil.getZincLayer() : "");
dto.setItemId(coil.getItemId() != null ? coil.getItemId().toString() : "");
dto.setActionCompleteTime(coil.getActionCompleteTime() != null ? sdf.format(coil.getActionCompleteTime()) : "");
dto.setTransferType(coil.getTransferType() != null ? coil.getTransferType() : "");
// 改判原因第26列
dto.setRejudgeReason(rejudgeReason != null ? rejudgeReason : "");
// 异常信息后17列
if (abnormal != null) {
dto.setProductionLine(abnormal.getProductionLine() != null ? abnormal.getProductionLine() : "");
dto.setPosition(abnormal.getPosition() != null ? abnormal.getPosition() : "");
dto.setAbnormalLength(abnormal.getLength() != null ? abnormal.getLength().toString() : "");
dto.setStartPosition(abnormal.getStartPosition() != null ? abnormal.getStartPosition().toString() : "");
dto.setEndPosition(abnormal.getEndPosition() != null ? abnormal.getEndPosition().toString() : "");
dto.setDefectCode(abnormal.getDefectCode() != null ? abnormal.getDefectCode() : "");
dto.setDefectType(abnormal.getDefectType() != null ? abnormal.getDefectType() : "");
dto.setDefectRate(abnormal.getDefectRate() != null ? abnormal.getDefectRate().toString() : "");
dto.setDefectWeight(abnormal.getDefectWeight() != null ? abnormal.getDefectWeight().toString() : "");
dto.setDegree(abnormal.getDegree() != null ? abnormal.getDegree() : "");
dto.setJudgeLevel(abnormal.getJudgeLevel() != null ? abnormal.getJudgeLevel() : "");
dto.setJudgeBy(abnormal.getJudgeBy() != null ? abnormal.getJudgeBy() : "");
dto.setJudgeTime(abnormal.getJudgeTime() != null ? sdf.format(abnormal.getJudgeTime()) : "");
dto.setMainMark(abnormal.getMainMark() != null ? abnormal.getMainMark().toString() : "");
dto.setWholeCoilMark(abnormal.getWholeCoilMark() != null ? abnormal.getWholeCoilMark().toString() : "");
dto.setAbnormalRemark(abnormal.getRemark() != null ? abnormal.getRemark() : "");
dto.setPlateSurface(abnormal.getPlateSurface() != null ? abnormal.getPlateSurface() : "");
} else {
// 空异常信息
dto.setProductionLine("");
dto.setPosition("");
dto.setAbnormalLength("");
dto.setStartPosition("");
dto.setEndPosition("");
dto.setDefectCode("");
dto.setDefectType("");
dto.setDefectRate("");
dto.setDefectWeight("");
dto.setDegree("");
dto.setJudgeLevel("");
dto.setJudgeBy("");
dto.setJudgeTime("");
dto.setMainMark("");
dto.setWholeCoilMark("");
dto.setAbnormalRemark("");
dto.setPlateSurface("");
}
return dto;
}
}