Explorar o código

更新excel数据读取

ghost %!s(int64=4) %!d(string=hai) anos
pai
achega
1b39faf47f

+ 17 - 1
forest-admin/admin-server/src/main/java/com/hwrj/cloud/admin/controller/PmsProductController.java

@@ -2,6 +2,8 @@ package com.hwrj.cloud.admin.controller;
 
 
 import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelReader;
+import com.alibaba.excel.read.metadata.ReadSheet;
 import com.hwrj.cloud.admin.common.PmsProductOutput;
 import com.hwrj.cloud.admin.dao.PmsProductExcelDao;
 import com.hwrj.cloud.admin.dto.param.*;
@@ -165,7 +167,21 @@ public class PmsProductController {
     @PostMapping(value = "/import")
     public CommonResult importBasic(MultipartFile file) throws IOException {
         PmsProductExcelListener basicExcelListener = new PmsProductExcelListener(productExcelDao);
-        EasyExcel.read(file.getInputStream(), PmsProductExcel.class, basicExcelListener).headRowNumber(1).sheet().doRead();
+//        EasyExcel.read(file.getInputStream(), PmsProductExcel.class, basicExcelListener).headRowNumber(1).sheet().doRead();
+
+        ExcelReader excelReader = null;
+        try {
+            excelReader = EasyExcel.read(file.getInputStream()).build();
+            ReadSheet readSheet2 =
+                    EasyExcel.readSheet(2).head(PmsProductExcel.class).registerReadListener(basicExcelListener).headRowNumber(2).build();
+            // 这里注意 一定要把sheet1 sheet2 一起传进去,不然有个问题就是03版的excel 会读取多次,浪费性能
+            excelReader.read(readSheet2);
+        } finally {
+            if (excelReader != null) {
+                // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
+                excelReader.finish();
+            }
+        }
         return CommonResult.success();
     }
 }

+ 19 - 1
forest-admin/admin-server/src/main/java/com/hwrj/cloud/admin/dto/param/PmsProductExcel.java

@@ -1,5 +1,6 @@
 package com.hwrj.cloud.admin.dto.param;
 
+import com.alibaba.excel.annotation.ExcelProperty;
 import lombok.Data;
 
 import java.io.Serializable;
@@ -11,67 +12,84 @@ public class PmsProductExcel implements Serializable {
     /**
      * 商品名称
      */
+    @ExcelProperty("产品名称")
     private String name;
     /**
      * 品牌名称
      */
+    @ExcelProperty("产品品牌")
     private String brandName;
     /**
      * 类型名称
      */
+    @ExcelProperty("类型")
     private String productCategoryName;
     /**
      * 商品图片
      */
+    @ExcelProperty("产品图片")
     private String pic;
     /**
      *规格
      */
+    @ExcelProperty("产品规格")
     private String spec;
     /**
      * 单位
      */
+    @ExcelProperty("包装")
     private String unit;
 
     /**
      * 产品标准代号
      * */
+    @ExcelProperty("产品标准代号")
     private String productSn;
     /**
      * 零售价格
      */
+    @ExcelProperty("零售价格(元)")
     private BigDecimal price;
     /**
      * 批发价
      */
+    @ExcelProperty("批发价格(元)")
     private BigDecimal tradePrice;
     /**
      * 生产日期
      */
-    private int dateOfManufacture;
+
+    @ExcelProperty("生产日期(年/月/日)")
+    private String dateOfManufacture;
     /**
      * 保质期
      */
+    @ExcelProperty("保质期")
     private String qualityGuaranteePeriod;
     /**
      * 生产地
      * */
+    @ExcelProperty("生产日期(年/月/日)")
     private String placeOfProduction;
     /**
      * 供应商
      */
+    @ExcelProperty("供应商")
     private String umsCompanyInfo;
     /**
      * 商品描述
      */
+    @ExcelProperty("产品简介")
     private String description;
 
     /**
      * 使用说明
      * */
+    @ExcelProperty("使用说明")
     private String instructions;
     /**
      * 注意事项
      * */
+    @ExcelProperty("注意事项")
     private String pointsForAttention;
 }

+ 1 - 1
forest-admin/admin-server/src/main/java/com/hwrj/cloud/admin/dto/param/PmsProductParam.java

@@ -210,7 +210,7 @@ public class PmsProductParam {
      * 生产日期
      **/
     @ExcelProperty("生产日期")
-    private int dateOfManufacture;
+    private String dateOfManufacture;
     /**
      * 生产地id
      **/