Просмотр исходного кода

feat:excel表格解析开发中

wangzaijun 6 месяцев назад
Родитель
Сommit
ae4eff6597

+ 18 - 0
service-base/src/main/java/com/simuwang/base/pojo/dto/report/CustomExcelTable.java

@@ -0,0 +1,18 @@
+package com.simuwang.base.pojo.dto.report;
+
+import com.simuwang.base.common.conts.Constants;
+import lombok.Getter;
+
+import java.io.Serial;
+import java.io.Serializable;
+
+/**
+ * @author wangzaijun
+ * @date 2024/10/14 8:52
+ * @description 自定义传递的表格对象,设置表格表格、列数和开始列索引
+ */
+@Getter
+public record CustomExcelTable(String title, int colCount) implements Serializable {
+    @Serial
+    private static final long serialVersionUID = Constants.DEFAULT_SERIAL_ID;
+}

+ 23 - 1
service-base/src/main/java/com/simuwang/base/pojo/dto/report/SimpleTable.java

@@ -5,6 +5,7 @@ import com.simuwang.base.common.conts.Constants;
 
 import java.io.Serial;
 import java.io.Serializable;
+import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 
@@ -24,14 +25,35 @@ public class SimpleTable implements Serializable {
      * 表格数据行
      */
     private final List<List<String>> rows;
+    /**
+     * 表格列数
+     */
+    private int colCount;
+    /**
+     * 表格行数
+     */
+    private int rowCount;
 
-    public SimpleTable(String title) {
+    public SimpleTable(String title, int colCount) {
         this.title = title;
+        this.colCount = colCount;
         this.rows = ListUtil.list(true);
     }
 
     public void addRow(List<String> row) {
         rows.add(row);
+        this.rowCount++;
+        if (this.colCount == 0) {
+            this.colCount = this.rows.stream().map(List::size).max(Comparator.naturalOrder()).orElse(0);
+        }
+    }
+
+    public int getColCount() {
+        return colCount;
+    }
+
+    public int getRowCount() {
+        return rowCount;
     }
 
     public String getTitle() {

+ 21 - 12
service-daq/src/main/java/com/simuwang/daq/components/CustomExcelMultiSheetListener.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.ListUtil;
 import com.alibaba.excel.context.AnalysisContext;
 import com.alibaba.excel.event.AnalysisEventListener;
 import com.alibaba.excel.read.metadata.holder.ReadSheetHolder;
+import com.simuwang.base.pojo.dto.report.CustomExcelTable;
 import com.simuwang.base.pojo.dto.report.SimpleTable;
 
 import java.util.LinkedHashMap;
@@ -14,7 +15,7 @@ import java.util.List;
  * @date 2024/10/12 9:17
  * @description 自定义的excel多sheet解析事件监听器
  */
-public class CustomExcelMultiSheetListener extends AnalysisEventListener<LinkedHashMap<String, Object>> {
+public class CustomExcelMultiSheetListener extends AnalysisEventListener<LinkedHashMap<Integer, Object>> {
     private final List<SimpleTable> tables = ListUtil.list(false);
     private SimpleTable table;
 
@@ -27,25 +28,33 @@ public class CustomExcelMultiSheetListener extends AnalysisEventListener<LinkedH
     }
 
     @Override
-    public void invoke(LinkedHashMap<String, Object> row, AnalysisContext analysisContext) {
+    public void invoke(LinkedHashMap<Integer, Object> row, AnalysisContext analysisContext) {
         ReadSheetHolder sheetHolder = analysisContext.readSheetHolder();
         String sheetName = sheetHolder.getSheetName();
         if (sheetName.contains("封面") && sheetHolder.getSheetNo() == 0) {
             return;
         }
         @SuppressWarnings("unchecked")
-        List<String> tableTitles = (List<String>) analysisContext.getCustom();
-        String title = ReportParseUtils.cleaningValue(row.get("1"));
-        if (tableTitles.contains(title)) {
-            this.table = new SimpleTable(title);
-            this.tables.add(table);
+        List<CustomExcelTable> customExcelTables = (List<CustomExcelTable>) analysisContext.getCustom();
+        String title = ReportParseUtils.cleaningValue(row.get(1));
+        if (title != null) {
+            for (CustomExcelTable customExcelTable : customExcelTables) {
+                String tableTitle = customExcelTable.getTitle();
+                if (title.equals(tableTitle) || title.contains(tableTitle)) {
+                    this.table = new SimpleTable(tableTitle, customExcelTable.getColCount());
+                    this.tables.add(this.table);
+                    return;
+                }
+            }
         }
-
-        List<String> tableRow = ListUtil.list(true);
-        for (int i = 1; i < row.size(); i++) {
-            tableRow.add(ReportParseUtils.cleaningValue(row.get(String.valueOf(i))));
+        if (this.table != null) {
+            List<String> tableRow = ListUtil.list(true);
+            int colCount = this.table.getColCount() <= 0 ? row.size() : this.table.getColCount();
+            for (int i = 1; i < colCount; i++) {
+                tableRow.add(ReportParseUtils.cleaningValue(row.get(i)));
+            }
+            this.table.addRow(tableRow);
         }
-        this.table.addRow(tableRow);
     }
 
     @Override

+ 3 - 2
service-daq/src/main/java/com/simuwang/daq/components/report/parser/excel/AbstractExcelReportParser.java

@@ -4,6 +4,7 @@ import com.alibaba.excel.EasyExcel;
 import com.alibaba.excel.read.builder.ExcelReaderBuilder;
 import com.simuwang.base.common.exception.ReportParseException;
 import com.simuwang.base.mapper.EmailFieldMappingMapper;
+import com.simuwang.base.pojo.dto.report.CustomExcelTable;
 import com.simuwang.base.pojo.dto.report.ReportData;
 import com.simuwang.base.pojo.dto.report.ReportParserParams;
 import com.simuwang.base.pojo.dto.report.SimpleTable;
@@ -25,7 +26,7 @@ public abstract class AbstractExcelReportParser<T extends ReportData> extends Ab
         CustomExcelMultiSheetListener excelListener = new CustomExcelMultiSheetListener();
         ExcelReaderBuilder readerBuilder = EasyExcel.read(params.getFilepath());
         readerBuilder.sheet();
-        readerBuilder.customObject(this.tableTitles());
+        readerBuilder.customObject(this.customExcelTables());
         readerBuilder.registerReadListener(excelListener);
         readerBuilder.doReadAll();
         // 解析出表格
@@ -40,7 +41,7 @@ public abstract class AbstractExcelReportParser<T extends ReportData> extends Ab
         // cleaning.
     }
 
-    protected abstract List<String> tableTitles();
+    protected abstract List<CustomExcelTable> customExcelTables();
 
     protected abstract T excelParse(List<SimpleTable> tables, ReportParserParams params);
 }

+ 7 - 2
service-daq/src/main/java/com/simuwang/daq/components/report/parser/excel/ExcelMonthlyReportParser.java

@@ -2,6 +2,7 @@ package com.simuwang.daq.components.report.parser.excel;
 
 import cn.hutool.core.collection.ListUtil;
 import com.simuwang.base.mapper.EmailFieldMappingMapper;
+import com.simuwang.base.pojo.dto.report.CustomExcelTable;
 import com.simuwang.base.pojo.dto.report.MonthlyReportData;
 import com.simuwang.base.pojo.dto.report.ReportParserParams;
 import com.simuwang.base.pojo.dto.report.SimpleTable;
@@ -17,8 +18,12 @@ public class ExcelMonthlyReportParser extends AbstractExcelReportParser<MonthlyR
     }
 
     @Override
-    protected List<String> tableTitles() {
-        return ListUtil.of("基金概况", "净值月报", "级基金净值表");
+    protected List<CustomExcelTable> customExcelTables() {
+        List<CustomExcelTable> customExcelTables = ListUtil.list(true);
+        customExcelTables.add(new CustomExcelTable("基金概况", 4));
+        customExcelTables.add(new CustomExcelTable("净值月报", 5));
+        customExcelTables.add(new CustomExcelTable("级基金净值表", 5));
+        return customExcelTables;
     }
 
     @Override