Kaynağa Gözat

调整导出表格格式

donglang 1 gün önce
ebeveyn
işleme
1f0cac024b

+ 99 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/handler/ExcelCustomRowStyleHandler.java

@@ -0,0 +1,99 @@
+package com.sckw.core.handler;
+
+import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.metadata.data.WriteCellData;
+import com.alibaba.excel.write.handler.CellWriteHandler;
+import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
+import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
+import org.apache.poi.ss.usermodel.*;
+
+import java.util.List;
+
+/**
+ * 自定义行样式处理器:支持隔行换色 + 特定条件高亮
+ */
+public class ExcelCustomRowStyleHandler implements CellWriteHandler {
+
+    // 定义颜色常量 (RGB)
+    private static final short COLOR_YELLOW = IndexedColors.YELLOW.getIndex(); // 黄色
+    private static final short COLOR_LIGHT_GRAY = IndexedColors.GREY_25_PERCENT.getIndex(); // 浅灰色(斑马纹)
+    private static final short COLOR_WHITE = IndexedColors.WHITE.getIndex(); // 白色
+
+    @Override
+    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
+                                 List<WriteCellData<?>> cellDataList, Cell cell, Head head,
+                                 Integer relativeRowIndex, Boolean isHead) {
+        // 1. 跳过表头,只处理数据行
+        if (isHead || relativeRowIndex == null) {
+            return;
+        }
+
+        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
+        Row row = cell.getRow();
+
+        // 2. 获取当前行的数据对象
+
+        // 假设ID是第一列;如果ID不是第一列,修改columnIndex判断
+        int idColumnIndex = 0;
+        boolean isSpecialRow = false;
+        long idValue = 0;
+
+        // 尝试获取当前单元格如果是ID列的值
+        if (cell.getColumnIndex() == idColumnIndex) {
+            try {
+                // 尝试读取数值
+                if (cell.getCellType() == CellType.NUMERIC) {
+                    idValue = (long) cell.getNumericCellValue();
+                } else if (cell.getCellType() == CellType.STRING) {
+                    idValue = Long.parseLong(cell.getStringCellValue());
+                }
+
+                // 判定条件:ID == -1
+                if (idValue == -1L) {
+                    isSpecialRow = true;
+                }
+            } catch (Exception e) {
+                // 忽略解析错误
+            }
+        } else {
+            // 如果不是ID列,我们需要查看同一行ID列的值来决定颜色
+            Cell idCell = row.getCell(idColumnIndex);
+            if (idCell != null) {
+                try {
+                    if (idCell.getCellType() == CellType.NUMERIC) {
+                        idValue = (long) idCell.getNumericCellValue();
+                    }
+                    if (idValue == -1L) isSpecialRow = true;
+                } catch(Exception ignored){}
+            }
+        }
+
+        // 3. 创建或获取样式
+        CellStyle style = workbook.createCellStyle();
+
+        // 4. 设置背景色逻辑
+        if (isSpecialRow) {
+            // 优先级最高:如果是 ID=-1,强制黄色
+            style.setFillForegroundColor(COLOR_YELLOW);
+            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+        } else {
+            // 否则执行隔行换色 (斑马纹)
+            // relativeRowIndex 从 0 开始,偶数行白,奇数行灰
+            if (relativeRowIndex % 2 == 1) {
+                style.setFillForegroundColor(COLOR_LIGHT_GRAY);
+                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+            } else {
+                style.setFillForegroundColor(COLOR_WHITE);
+                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
+            }
+        }
+
+        // 5. 将样式应用到当前单元格
+        style.setBorderBottom(BorderStyle.THIN);
+        style.setBorderLeft(BorderStyle.THIN);
+        style.setBorderRight(BorderStyle.THIN);
+        style.setBorderTop(BorderStyle.THIN);
+
+        cell.setCellStyle(style);
+    }
+}

+ 2 - 1
sckw-common/sckw-common-excel/src/main/java/com/sckw/excel/utils/ExcelUtil.java

@@ -6,6 +6,7 @@ import com.alibaba.excel.annotation.ExcelProperty;
 import com.alibaba.excel.write.metadata.WriteSheet;
 import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
 import com.alibaba.fastjson.JSONObject;
+import com.sckw.core.handler.ExcelCustomRowStyleHandler;
 import com.sckw.core.model.constant.NumberConstant;
 import com.sckw.core.model.constant.Global;
 import com.sckw.core.web.response.HttpResult;
@@ -58,7 +59,7 @@ public class ExcelUtil {
                     //设置导出excel的单元格格式为文本
                     .registerWriteHandler(new RowWriteHandlerImpl())
                     // 自定义列宽、行高
-                    .registerWriteHandler(new CustomCellWeightWeightConfig())
+                    .registerWriteHandler(new ExcelCustomRowStyleHandler())
                     .sheet(excelContext.sheetName()).doWrite(data);
         } catch (Exception e) {
             buildResponse(response, e);

+ 0 - 16
sckw-modules/sckw-transport/src/main/java/com/sckw/transport/model/vo/WaybillOrderReportRespExcelVO.java

@@ -29,11 +29,6 @@ public class WaybillOrderReportRespExcelVO implements Serializable {
     @ExcelProperty(value = "序号" )
     private Long id;
 
-    /**
-     * 采购单位id
-     */
-    private Long ProcurementEntId;
-
     /**
      * 采购单位名称
      */
@@ -52,11 +47,6 @@ public class WaybillOrderReportRespExcelVO implements Serializable {
     @ExcelProperty(value = "运单号" )
     private String waybillNo;
 
-    /**
-     * 商品id
-     */
-    private Long goodsId;
-
     /**
      * 商品名称
      */
@@ -112,10 +102,4 @@ public class WaybillOrderReportRespExcelVO implements Serializable {
     @ExcelProperty(value = "金额(元)")
     private BigDecimal totalPrice;
 
-    /**
-     * 剩余预付款
-     */
-
-    private BigDecimal advancePayment;
-
 }