yangyb 1 lună în urmă
părinte
comite
d517678188

+ 9 - 9
mall-service/src/main/java/com/txz/mall/controller/StoreProductController.java

@@ -24,7 +24,7 @@ import java.util.List;
 /**
  * Created by CodeGenerator on 2025/07/10.
  */
-@Api(tags = "[后台]storeProduct管理")
+@Api(tags = "[后台]商品管理")
 @RestController
 @RequestMapping("/store/product")
 public class StoreProductController {
@@ -35,7 +35,7 @@ public class StoreProductController {
     private StoreProductService storeProductService;
 
     @PostMapping("/add")
-    @ApiOperation(value = "storeProduct新增", httpMethod = "POST")
+    @ApiOperation(value = "商品新增", httpMethod = "POST")
     public Result add(@RequestBody StoreProduct storeProduct, Long userId) {
         if (storeProduct == null) {
             return Result.fail(ResultCode.OBJECT_IS_NULL);
@@ -55,7 +55,7 @@ public class StoreProductController {
     }
 
     @PostMapping("/delete")
-    @ApiOperation(value = "storeProduct删除", httpMethod = "POST")
+    @ApiOperation(value = "商品删除", httpMethod = "POST")
     public Result delete(@RequestParam Long id, Long userId) {
         if (id == null) {
             return Result.fail(ResultCode.ID_IS_NULL);
@@ -75,7 +75,7 @@ public class StoreProductController {
     }
 
     @PostMapping("/update")
-    @ApiOperation(value = "storeProduct更新", httpMethod = "POST")
+    @ApiOperation(value = "商品更新", httpMethod = "POST")
     public Result update(@RequestBody StoreProduct storeProduct, Long userId) {
         if (storeProduct == null) {
             return Result.fail(ResultCode.OBJECT_IS_NULL);
@@ -98,7 +98,7 @@ public class StoreProductController {
     }
 
     @PostMapping("/updateShowStatus")
-    @ApiOperation(value = "storeProduct上下架", httpMethod = "POST")
+    @ApiOperation(value = "商品上下架", httpMethod = "POST")
     public Result updateShowStatus(@RequestParam Long id, @RequestParam Boolean ShowStatus, Long userId) {
         if (ShowStatus == null) {
             return Result.fail(ResultCode.OBJECT_IS_NULL);
@@ -123,7 +123,7 @@ public class StoreProductController {
     }
 
     @PostMapping("/detail")
-    @ApiOperation(value = "storeProduct获取详情", httpMethod = "POST")
+    @ApiOperation(value = "商品获取详情", httpMethod = "POST")
     public Result<StoreProduct> detail(@RequestParam Long id, Long userId) {
         if (id == null) {
             return Result.fail(ResultCode.ID_IS_NULL);
@@ -142,7 +142,7 @@ public class StoreProductController {
     }
 
     @PostMapping("/list")
-    @ApiOperation(value = "storeProduct获取列表", httpMethod = "POST")
+    @ApiOperation(value = "商品获取列表", httpMethod = "POST")
     public Result<List<StoreProduct>> list(@RequestBody StoreProduct storeProduct, @RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size, Long userId) {
         if (userId == null) {
             return Result.fail(ResultCode.USERID_IS_NULL);
@@ -164,14 +164,14 @@ public class StoreProductController {
     }
 
 
-    @ApiOperation("storeProduct导入")
+    @ApiOperation("商品导入")
     @PostMapping("/import")
     public Result importFile(@JSONField(serialize = false) @RequestParam("file") MultipartFile file) {
         storeProductService.importFile(file);
         return Result.success();
     }
 
-    @ApiOperation("storeProduct模版")
+    @ApiOperation("商品导入模版")
     @PostMapping("/export")
     public Result exportFile(HttpServletResponse response) {
         storeProductService.exportFile(response);

+ 8 - 5
mall-service/src/main/java/com/txz/mall/service/impl/StoreProductServiceImpl.java

@@ -1,11 +1,14 @@
 package com.txz.mall.service.impl;
 
 import cn.hutool.core.io.FileUtil;
+import com.alibaba.excel.EasyExcel;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.txz.mall.core.AbstractService;
 import com.txz.mall.core.ServiceException;
 import com.txz.mall.dao.StoreProductMapper;
 import com.txz.mall.model.StoreProduct;
 import com.txz.mall.service.StoreProductService;
+import com.txz.mall.util.EasyExcelUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -38,10 +41,10 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             throw new ServiceException("请传入xlsx或xls文档格式文件");
         }
         try {
-//            List<StoreProduct> list = EasyExcelUtil.syncReadModel(file.getInputStream(), StoreProduct.class, 0, 1);
-//            if (CollectionUtils.isEmpty(list)) {
-//                throw new ServiceException("请填写内容后再提交!");
-//            }
+            List<StoreProduct> list = EasyExcelUtil.syncReadModel(file.getInputStream(), StoreProduct.class, 0, 1);
+            if (CollectionUtils.isEmpty(list)) {
+                throw new ServiceException("请填写内容后再提交!");
+            }
 
         } catch (Exception e) {
             throw new RuntimeException(e);
@@ -59,7 +62,7 @@ public class StoreProductServiceImpl extends AbstractService<StoreProduct> imple
             response.setHeader(
                     "Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
             List<StoreProduct> list = new ArrayList<>();
-//            EasyExcel.write(outputStream, StoreProduct.class).sheet("商品导出模板").doWrite(list);
+            EasyExcel.write(outputStream, StoreProduct.class).sheet("商品导出模板").doWrite(list);
         } catch (Exception e) {
             log.error("模板下载失败", e);
             throw new ServiceException("模板下载失败, 请联系管理员");

+ 475 - 475
mall-service/src/main/java/com/txz/mall/util/EasyExcelUtil.java

@@ -1,475 +1,475 @@
-//package com.txz.mall.util;
-//
-//import com.alibaba.excel.EasyExcel;
-//import com.alibaba.excel.EasyExcelFactory;
-//import com.alibaba.excel.context.AnalysisContext;
-//import com.alibaba.excel.event.AnalysisEventListener;
-//import com.alibaba.excel.exception.ExcelDataConvertException;
-//import com.alibaba.excel.write.handler.WriteHandler;
-//import com.alibaba.fastjson.JSON;
-//import com.txz.mall.util.factory.EasyExcelWriterFactory;
-//import lombok.extern.slf4j.Slf4j;
-//import org.apache.poi.ss.formula.functions.T;
-//
-//import javax.servlet.http.HttpServletResponse;
-//import java.io.File;
-//import java.io.IOException;
-//import java.io.InputStream;
-//import java.io.OutputStream;
-//import java.net.URLEncoder;
-//import java.util.ArrayList;
-//import java.util.List;
-//import java.util.Map;
-//import java.util.Set;
-//
-//;
-//
-///**
-// * @author: admin
-// * @date: 2022/10/28
-// * @time: 17:07
-// * @description: EasyExcel工具类
-// */
-//@Slf4j
-//public class EasyExcelUtil {
-//
-//    /**
-//     * 同步无模型读(默认读取sheet0,从第2行开始读)
-//     * @param filePath
-//     * @return
-//     */
-//    public static List<Map<Integer, String>> syncRead(String filePath) {
-//        return EasyExcelFactory.read(filePath).sheet().doReadSync();
-//    }
-//
-//    /**
-//     * 同步无模型读(默认表头占一行,从第2行开始读)
-//     *
-//     * @param filePath
-//     * @param sheetNo  sheet页号,从0开始
-//     * @return
-//     */
-//    public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo) {
-//        return EasyExcelFactory.read(filePath).sheet(sheetNo).doReadSync();
-//    }
-//
-//    /**
-//     * 同步无模型读(指定sheet和表头占的行数)
-//     * @param inputStream
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return List<Map < colNum, cellValue>>
-//     */
-//    public static List<Map<Integer, String>> syncRead(InputStream inputStream, Integer sheetNo, Integer headRowNum) {
-//        return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
-//    }
-//
-//    /**
-//     * 同步无模型读(指定sheet和表头占的行数)
-//     * @param file
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return List<Map < colNum, cellValue>>
-//     */
-//    public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo, Integer headRowNum) {
-//        return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
-//    }
-//
-//    /**
-//     * 同步无模型读(指定sheet和表头占的行数)
-//     * @param filePath
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return List<Map < colNum, cellValue>>
-//     */
-//    public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo, Integer headRowNum) {
-//        return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
-//    }
-//
-//    /**
-//     * 同步按模型读(默认读取sheet0,从第2行开始读)
-//     * @param filePath
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @return
-//     */
-//    public static List<T> syncReadModel(String filePath, Class clazz) {
-//        return EasyExcelFactory.read(filePath).sheet().head(clazz).doReadSync();
-//    }
-//
-//    /**
-//     * 同步按模型读(默认表头占一行,从第2行开始读)
-//     * @param filePath
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo sheet页号,从0开始
-//     * @return
-//     */
-//    public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo) {
-//        return EasyExcelFactory.read(filePath).sheet(sheetNo).head(clazz).doReadSync();
-//    }
-//
-//    /**
-//     * 同步按模型读(指定sheet和表头占的行数)
-//     * @param inputStream
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static <T> List<T> syncReadModel(InputStream inputStream, Class<T> clazz, Integer sheetNo, Integer headRowNum) {
-//        return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
-//    }
-//
-//    /**
-//     * 同步按模型读(指定sheet和表头占的行数)
-//     * @param file
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static <T> List<T> syncReadModel(File file, Class<T> clazz, Integer sheetNo, Integer headRowNum) {
-//        return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
-//    }
-//
-//    /**
-//     * 同步按模型读(指定sheet和表头占的行数)
-//     * @param filePath
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo, Integer headRowNum) {
-//        return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
-//    }
-//
-//    /**
-//     * 异步无模型读(默认读取sheet0,从第2行开始读)
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener) {
-//        EasyExcelFactory.read(filePath, excelListener).sheet().doRead();
-//    }
-//
-//    /**
-//     * 异步无模型读(默认表头占一行,从第2行开始读)
-//     * @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param sheetNo sheet页号,从0开始
-//     * @return
-//     */
-//    public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo) {
-//        EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).doRead();
-//    }
-//
-//    /**
-//     * 异步无模型读(指定sheet和表头占的行数)
-//     * @param inputStream
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
-//        EasyExcelFactory.read(inputStream, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
-//    }
-//
-//    /**
-//     * 异步无模型读(指定sheet和表头占的行数)
-//     * @param file
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static void asyncRead(File file, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
-//        EasyExcelFactory.read(file, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
-//    }
-//
-//    /**
-//     * 异步无模型读(指定sheet和表头占的行数)
-//     * @param filePath
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param sheetNo sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     * @return
-//     */
-//    public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
-//        EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
-//    }
-//
-//    /**
-//     * 异步按模型读取(默认读取sheet0,从第2行开始读)
-//     * @param filePath
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     */
-//    public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz) {
-//        EasyExcelFactory.read(filePath, clazz, excelListener).sheet().doRead();
-//    }
-//
-//    /**
-//     * 异步按模型读取(默认表头占一行,从第2行开始读)
-//     * @param filePath
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo  sheet页号,从0开始
-//     */
-//    public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo) {
-//        EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).doRead();
-//    }
-//
-//    /**
-//     * 异步按模型读取
-//     * @param inputStream
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo  sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     */
-//    public static void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
-//        EasyExcelFactory.read(inputStream, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
-//    }
-//
-//    /**
-//     * 异步按模型读取
-//     * @param file
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo  sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     */
-//    public static void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
-//        EasyExcelFactory.read(file, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
-//    }
-//
-//    /**
-//     * 异步按模型读取
-//     * @param filePath
-//     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
-//     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
-//     * @param sheetNo  sheet页号,从0开始
-//     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
-//     */
-//    public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
-//        EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
-//    }
-//
-//    /**
-//     * 无模板写文件
-//     * @param filePath
-//     * @param head 表头数据
-//     * @param data 表内容数据
-//     */
-//    public static void write(String filePath, List<List<String>> head, List<List<Object>> data) {
-//        EasyExcel.write(filePath).head(head).sheet().doWrite(data);
-//    }
-//
-//    /**
-//     * 无模板写文件
-//     * @param filePath
-//     * @param head 表头数据
-//     * @param data 表内容数据
-//     * @param sheetNo sheet页号,从0开始
-//     * @param sheetName sheet名称
-//     */
-//    public static void write(String filePath, List<List<String>> head, List<List<Object>> data, Integer sheetNo, String sheetName) {
-//        EasyExcel.write(filePath).head(head).sheet(sheetNo, sheetName).doWrite(data);
-//    }
-//
-//    /**
-//     * 根据excel模板文件写入文件
-//     * @param filePath
-//     * @param templateFileName
-//     * @param headClazz
-//     * @param data
-//     */
-//    public static void writeTemplate(String filePath, String templateFileName, Class headClazz, List data) {
-//        EasyExcel.write(filePath, headClazz).withTemplate(templateFileName).sheet().doWrite(data);
-//    }
-//
-//    /**
-//     * 根据excel模板文件写入文件
-//     * @param filePath
-//     * @param templateFileName
-//     * @param data
-//     */
-//    public static void writeTemplate(String filePath, String templateFileName, List data) {
-//        EasyExcel.write(filePath).withTemplate(templateFileName).sheet().doWrite(data);
-//    }
-//
-//    /**
-//     * 按模板写文件
-//     * @param filePath
-//     * @param headClazz 表头模板
-//     * @param data 数据
-//     */
-//    public static void write(String filePath, Class headClazz, List data) {
-//        EasyExcel.write(filePath, headClazz).sheet().doWrite(data);
-//    }
-//
-//    /**
-//     * 按模板写文件
-//     * @param filePath
-//     * @param headClazz 表头模板
-//     * @param data 数据
-//     * @param sheetNo sheet页号,从0开始
-//     * @param sheetName sheet名称
-//     */
-//    public static void write(String filePath, Class headClazz, List data, Integer sheetNo, String sheetName) {
-//        EasyExcel.write(filePath, headClazz).sheet(sheetNo, sheetName).doWrite(data);
-//    }
-//
-//    /**
-//     * 按模板写文件
-//     * @param filePath
-//     * @param headClazz 表头模板
-//     * @param data 数据
-//     * @param writeHandler 自定义的处理器,比如设置table样式,设置超链接、单元格下拉框等等功能都可以通过这个实现(需要注册多个则自己通过链式去调用)
-//     * @param sheetNo sheet页号,从0开始
-//     * @param sheetName sheet名称
-//     */
-//    public static void write(String filePath, Class headClazz, List data, WriteHandler writeHandler, Integer sheetNo, String sheetName) {
-//        EasyExcel.write(filePath, headClazz).registerWriteHandler(writeHandler).sheet(sheetNo, sheetName).doWrite(data);
-//    }
-//
-//    /**
-//     * 按模板写文件(包含某些字段)
-//     * @param filePath
-//     * @param headClazz 表头模板
-//     * @param data 数据
-//     * @param includeCols 过滤包含的字段,根据字段名称过滤
-//     * @param sheetNo sheet页号,从0开始
-//     * @param sheetName sheet名称
-//     */
-//    public static void writeInclude(String filePath, Class headClazz, List data, Set<String> includeCols, Integer sheetNo, String sheetName) {
-//        EasyExcel.write(filePath, headClazz).includeColumnFiledNames(includeCols).sheet(sheetNo, sheetName).doWrite(data);
-//    }
-//
-//    /**
-//     * 按模板写文件(排除某些字段)
-//     * @param filePath
-//     * @param headClazz 表头模板
-//     * @param data 数据
-//     * @param excludeCols 过滤排除的字段,根据字段名称过滤
-//     * @param sheetNo sheet页号,从0开始
-//     * @param sheetName sheet名称
-//     */
-//    public static void writeExclude(String filePath, Class headClazz, List data, Set<String> excludeCols, Integer sheetNo, String sheetName) {
-//        EasyExcel.write(filePath, headClazz).excludeColumnFiledNames(excludeCols).sheet(sheetNo, sheetName).doWrite(data);
-//    }
-//
-//    /**
-//     * 多个sheet页的数据链式写入
-//     * ExcelUtil.writeWithSheets(outputStream)
-//     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
-//     *                 .write(headData, data,"sheetName2")
-//     *                 .finish();
-//     * @param outputStream
-//     * @return
-//     */
-//    public static EasyExcelWriterFactory writeWithSheets(OutputStream outputStream) {
-//        EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(outputStream);
-//        return excelWriter;
-//    }
-//
-//    /**
-//     * 多个sheet页的数据链式写入
-//     * ExcelUtil.writeWithSheets(file)
-//     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
-//     *                 .write(headData, data,"sheetName2")
-//     *                 .finish();
-//     * @param file
-//     * @return
-//     */
-//    public static EasyExcelWriterFactory writeWithSheets(File file) {
-//        EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(file);
-//        return excelWriter;
-//    }
-//
-//    /**
-//     * 多个sheet页的数据链式写入
-//     * ExcelUtil.writeWithSheets(filePath)
-//     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
-//     *                 .write(headData, data,"sheetName2")
-//     *                 .finish();
-//     * @param filePath
-//     * @return
-//     */
-//    public static EasyExcelWriterFactory writeWithSheets(String filePath) {
-//        EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(filePath);
-//        return excelWriter;
-//    }
-//
-//    /**
-//     * 多个sheet页的数据链式写入(失败了会返回一个有部分数据的Excel)
-//     * ExcelUtil.writeWithSheets(response, exportFileName)
-//     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
-//     *                 .write(headData, data,"sheetName2")
-//     *                 .finish();
-//     * @param response
-//     * @param exportFileName 导出的文件名称
-//     * @return
-//     */
-//    public static EasyExcelWriterFactory writeWithSheetsWeb(HttpServletResponse response, String exportFileName) throws IOException, IOException {
-//        response.setContentType("application/vnd.ms-excel");
-////        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-//        response.setCharacterEncoding("utf-8");
-//        // 这里URLEncoder.encode可以防止中文乱码
-//        String fileName = URLEncoder.encode(exportFileName, "UTF-8");
-//        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
-//        return new EasyExcelWriterFactory(response.getOutputStream());
-//    }
-//
-//
-//
-//}
-//
-///**
-// * 默认按模型读取的监听器
-// * @param <T>
-// */
-//@Slf4j
-//class DefaultExcelListener<T> extends AnalysisEventListener<T> {
-//    private final List<T> rows = new ArrayList();
-//
-//    @Override
-//    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
-//        log.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
-//    }
-//
-//    @Override
-//    public void invoke(T object, AnalysisContext context) {
-//        rows.add(object);
-//        // 实际数据量比较大时,rows里的数据可以存到一定量之后进行批量处理(比如存到数据库),
-//        // 然后清空列表,以防止内存占用过多造成OOM
-//    }
-//
-//    @Override
-//    public void doAfterAllAnalysed(AnalysisContext context) {
-//        log.info("read {} rows", rows.size());
-//    }
-//
-//    /**
-//     * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
-//     *
-//     * @param exception
-//     * @param context
-//     * @throws Exception
-//     */
-//    @Override
-//    public void onException(Exception exception, AnalysisContext context) {
-//        log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
-//        if (exception instanceof ExcelDataConvertException) {
-//            ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
-//            log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(),
-//                    excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
-//        }
-//    }
-//
-//    public List<T> getRows() {
-//        return rows;
-//    }
-//}
+package com.txz.mall.util;
+
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.EasyExcelFactory;
+import com.alibaba.excel.context.AnalysisContext;
+import com.alibaba.excel.event.AnalysisEventListener;
+import com.alibaba.excel.exception.ExcelDataConvertException;
+import com.alibaba.excel.write.handler.WriteHandler;
+import com.alibaba.fastjson.JSON;
+import com.txz.mall.util.factory.EasyExcelWriterFactory;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.poi.ss.formula.functions.T;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+;
+
+/**
+ * @author: admin
+ * @date: 2022/10/28
+ * @time: 17:07
+ * @description: EasyExcel工具类
+ */
+@Slf4j
+public class EasyExcelUtil {
+
+    /**
+     * 同步无模型读(默认读取sheet0,从第2行开始读)
+     * @param filePath
+     * @return
+     */
+    public static List<Map<Integer, String>> syncRead(String filePath) {
+        return EasyExcelFactory.read(filePath).sheet().doReadSync();
+    }
+
+    /**
+     * 同步无模型读(默认表头占一行,从第2行开始读)
+     *
+     * @param filePath
+     * @param sheetNo  sheet页号,从0开始
+     * @return
+     */
+    public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo) {
+        return EasyExcelFactory.read(filePath).sheet(sheetNo).doReadSync();
+    }
+
+    /**
+     * 同步无模型读(指定sheet和表头占的行数)
+     * @param inputStream
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return List<Map < colNum, cellValue>>
+     */
+    public static List<Map<Integer, String>> syncRead(InputStream inputStream, Integer sheetNo, Integer headRowNum) {
+        return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
+    }
+
+    /**
+     * 同步无模型读(指定sheet和表头占的行数)
+     * @param file
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return List<Map < colNum, cellValue>>
+     */
+    public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo, Integer headRowNum) {
+        return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
+    }
+
+    /**
+     * 同步无模型读(指定sheet和表头占的行数)
+     * @param filePath
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return List<Map < colNum, cellValue>>
+     */
+    public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo, Integer headRowNum) {
+        return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
+    }
+
+    /**
+     * 同步按模型读(默认读取sheet0,从第2行开始读)
+     * @param filePath
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @return
+     */
+    public static List<T> syncReadModel(String filePath, Class clazz) {
+        return EasyExcelFactory.read(filePath).sheet().head(clazz).doReadSync();
+    }
+
+    /**
+     * 同步按模型读(默认表头占一行,从第2行开始读)
+     * @param filePath
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo sheet页号,从0开始
+     * @return
+     */
+    public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo) {
+        return EasyExcelFactory.read(filePath).sheet(sheetNo).head(clazz).doReadSync();
+    }
+
+    /**
+     * 同步按模型读(指定sheet和表头占的行数)
+     * @param inputStream
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static <T> List<T> syncReadModel(InputStream inputStream, Class<T> clazz, Integer sheetNo, Integer headRowNum) {
+        return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
+    }
+
+    /**
+     * 同步按模型读(指定sheet和表头占的行数)
+     * @param file
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static <T> List<T> syncReadModel(File file, Class<T> clazz, Integer sheetNo, Integer headRowNum) {
+        return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
+    }
+
+    /**
+     * 同步按模型读(指定sheet和表头占的行数)
+     * @param filePath
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo, Integer headRowNum) {
+        return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
+    }
+
+    /**
+     * 异步无模型读(默认读取sheet0,从第2行开始读)
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener) {
+        EasyExcelFactory.read(filePath, excelListener).sheet().doRead();
+    }
+
+    /**
+     * 异步无模型读(默认表头占一行,从第2行开始读)
+     * @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param sheetNo sheet页号,从0开始
+     * @return
+     */
+    public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo) {
+        EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).doRead();
+    }
+
+    /**
+     * 异步无模型读(指定sheet和表头占的行数)
+     * @param inputStream
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
+        EasyExcelFactory.read(inputStream, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
+    }
+
+    /**
+     * 异步无模型读(指定sheet和表头占的行数)
+     * @param file
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static void asyncRead(File file, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
+        EasyExcelFactory.read(file, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
+    }
+
+    /**
+     * 异步无模型读(指定sheet和表头占的行数)
+     * @param filePath
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param sheetNo sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     * @return
+     */
+    public static void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
+        EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
+    }
+
+    /**
+     * 异步按模型读取(默认读取sheet0,从第2行开始读)
+     * @param filePath
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     */
+    public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz) {
+        EasyExcelFactory.read(filePath, clazz, excelListener).sheet().doRead();
+    }
+
+    /**
+     * 异步按模型读取(默认表头占一行,从第2行开始读)
+     * @param filePath
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo  sheet页号,从0开始
+     */
+    public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo) {
+        EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).doRead();
+    }
+
+    /**
+     * 异步按模型读取
+     * @param inputStream
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo  sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     */
+    public static void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
+        EasyExcelFactory.read(inputStream, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
+    }
+
+    /**
+     * 异步按模型读取
+     * @param file
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo  sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     */
+    public static void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
+        EasyExcelFactory.read(file, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
+    }
+
+    /**
+     * 异步按模型读取
+     * @param filePath
+     * @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
+     * @param clazz 模型的类类型(excel数据会按该类型转换成对象)
+     * @param sheetNo  sheet页号,从0开始
+     * @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
+     */
+    public static void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
+        EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
+    }
+
+    /**
+     * 无模板写文件
+     * @param filePath
+     * @param head 表头数据
+     * @param data 表内容数据
+     */
+    public static void write(String filePath, List<List<String>> head, List<List<Object>> data) {
+        EasyExcel.write(filePath).head(head).sheet().doWrite(data);
+    }
+
+    /**
+     * 无模板写文件
+     * @param filePath
+     * @param head 表头数据
+     * @param data 表内容数据
+     * @param sheetNo sheet页号,从0开始
+     * @param sheetName sheet名称
+     */
+    public static void write(String filePath, List<List<String>> head, List<List<Object>> data, Integer sheetNo, String sheetName) {
+        EasyExcel.write(filePath).head(head).sheet(sheetNo, sheetName).doWrite(data);
+    }
+
+    /**
+     * 根据excel模板文件写入文件
+     * @param filePath
+     * @param templateFileName
+     * @param headClazz
+     * @param data
+     */
+    public static void writeTemplate(String filePath, String templateFileName, Class headClazz, List data) {
+        EasyExcel.write(filePath, headClazz).withTemplate(templateFileName).sheet().doWrite(data);
+    }
+
+    /**
+     * 根据excel模板文件写入文件
+     * @param filePath
+     * @param templateFileName
+     * @param data
+     */
+    public static void writeTemplate(String filePath, String templateFileName, List data) {
+        EasyExcel.write(filePath).withTemplate(templateFileName).sheet().doWrite(data);
+    }
+
+    /**
+     * 按模板写文件
+     * @param filePath
+     * @param headClazz 表头模板
+     * @param data 数据
+     */
+    public static void write(String filePath, Class headClazz, List data) {
+        EasyExcel.write(filePath, headClazz).sheet().doWrite(data);
+    }
+
+    /**
+     * 按模板写文件
+     * @param filePath
+     * @param headClazz 表头模板
+     * @param data 数据
+     * @param sheetNo sheet页号,从0开始
+     * @param sheetName sheet名称
+     */
+    public static void write(String filePath, Class headClazz, List data, Integer sheetNo, String sheetName) {
+        EasyExcel.write(filePath, headClazz).sheet(sheetNo, sheetName).doWrite(data);
+    }
+
+    /**
+     * 按模板写文件
+     * @param filePath
+     * @param headClazz 表头模板
+     * @param data 数据
+     * @param writeHandler 自定义的处理器,比如设置table样式,设置超链接、单元格下拉框等等功能都可以通过这个实现(需要注册多个则自己通过链式去调用)
+     * @param sheetNo sheet页号,从0开始
+     * @param sheetName sheet名称
+     */
+    public static void write(String filePath, Class headClazz, List data, WriteHandler writeHandler, Integer sheetNo, String sheetName) {
+        EasyExcel.write(filePath, headClazz).registerWriteHandler(writeHandler).sheet(sheetNo, sheetName).doWrite(data);
+    }
+
+    /**
+     * 按模板写文件(包含某些字段)
+     * @param filePath
+     * @param headClazz 表头模板
+     * @param data 数据
+     * @param includeCols 过滤包含的字段,根据字段名称过滤
+     * @param sheetNo sheet页号,从0开始
+     * @param sheetName sheet名称
+     */
+    public static void writeInclude(String filePath, Class headClazz, List data, Set<String> includeCols, Integer sheetNo, String sheetName) {
+        EasyExcel.write(filePath, headClazz).includeColumnFiledNames(includeCols).sheet(sheetNo, sheetName).doWrite(data);
+    }
+
+    /**
+     * 按模板写文件(排除某些字段)
+     * @param filePath
+     * @param headClazz 表头模板
+     * @param data 数据
+     * @param excludeCols 过滤排除的字段,根据字段名称过滤
+     * @param sheetNo sheet页号,从0开始
+     * @param sheetName sheet名称
+     */
+    public static void writeExclude(String filePath, Class headClazz, List data, Set<String> excludeCols, Integer sheetNo, String sheetName) {
+        EasyExcel.write(filePath, headClazz).excludeColumnFiledNames(excludeCols).sheet(sheetNo, sheetName).doWrite(data);
+    }
+
+    /**
+     * 多个sheet页的数据链式写入
+     * ExcelUtil.writeWithSheets(outputStream)
+     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
+     *                 .write(headData, data,"sheetName2")
+     *                 .finish();
+     * @param outputStream
+     * @return
+     */
+    public static EasyExcelWriterFactory writeWithSheets(OutputStream outputStream) {
+        EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(outputStream);
+        return excelWriter;
+    }
+
+    /**
+     * 多个sheet页的数据链式写入
+     * ExcelUtil.writeWithSheets(file)
+     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
+     *                 .write(headData, data,"sheetName2")
+     *                 .finish();
+     * @param file
+     * @return
+     */
+    public static EasyExcelWriterFactory writeWithSheets(File file) {
+        EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(file);
+        return excelWriter;
+    }
+
+    /**
+     * 多个sheet页的数据链式写入
+     * ExcelUtil.writeWithSheets(filePath)
+     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
+     *                 .write(headData, data,"sheetName2")
+     *                 .finish();
+     * @param filePath
+     * @return
+     */
+    public static EasyExcelWriterFactory writeWithSheets(String filePath) {
+        EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(filePath);
+        return excelWriter;
+    }
+
+    /**
+     * 多个sheet页的数据链式写入(失败了会返回一个有部分数据的Excel)
+     * ExcelUtil.writeWithSheets(response, exportFileName)
+     *                 .writeModel(ExcelModel.class, excelModelList, "sheetName1")
+     *                 .write(headData, data,"sheetName2")
+     *                 .finish();
+     * @param response
+     * @param exportFileName 导出的文件名称
+     * @return
+     */
+    public static EasyExcelWriterFactory writeWithSheetsWeb(HttpServletResponse response, String exportFileName) throws IOException, IOException {
+        response.setContentType("application/vnd.ms-excel");
+//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+        response.setCharacterEncoding("utf-8");
+        // 这里URLEncoder.encode可以防止中文乱码
+        String fileName = URLEncoder.encode(exportFileName, "UTF-8");
+        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
+        return new EasyExcelWriterFactory(response.getOutputStream());
+    }
+
+
+
+}
+
+/**
+ * 默认按模型读取的监听器
+ * @param <T>
+ */
+@Slf4j
+class DefaultExcelListener<T> extends AnalysisEventListener<T> {
+    private final List<T> rows = new ArrayList();
+
+    @Override
+    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
+        log.info("解析到一条头数据:{}", JSON.toJSONString(headMap));
+    }
+
+    @Override
+    public void invoke(T object, AnalysisContext context) {
+        rows.add(object);
+        // 实际数据量比较大时,rows里的数据可以存到一定量之后进行批量处理(比如存到数据库),
+        // 然后清空列表,以防止内存占用过多造成OOM
+    }
+
+    @Override
+    public void doAfterAllAnalysed(AnalysisContext context) {
+        log.info("read {} rows", rows.size());
+    }
+
+    /**
+     * 在转换异常 获取其他异常下会调用本接口。抛出异常则停止读取。如果这里不抛出异常则 继续读取下一行。
+     *
+     * @param exception
+     * @param context
+     * @throws Exception
+     */
+    @Override
+    public void onException(Exception exception, AnalysisContext context) {
+        log.error("解析失败,但是继续解析下一行:{}", exception.getMessage());
+        if (exception instanceof ExcelDataConvertException) {
+            ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
+            log.error("第{}行,第{}列解析异常,数据为:{}", excelDataConvertException.getRowIndex(),
+                    excelDataConvertException.getColumnIndex(), excelDataConvertException.getCellData());
+        }
+    }
+
+    public List<T> getRows() {
+        return rows;
+    }
+}

+ 78 - 78
mall-service/src/main/java/com/txz/mall/util/factory/EasyExcelWriterFactory.java

@@ -1,78 +1,78 @@
-//package com.txz.mall.util.factory;
-//
-//import com.alibaba.excel.EasyExcel;
-//import com.alibaba.excel.ExcelWriter;
-//
-//import java.io.File;
-//import java.io.OutputStream;
-//import java.util.List;
-//
-///**
-// * @author: admin
-// * @date: 2022/10/28
-// * @time: 17:12
-// * @description: 自定义EasyExcel写工厂
-// */
-//public class EasyExcelWriterFactory {
-//    private int sheetNo = 0;
-//    private ExcelWriter excelWriter = null;
-//
-//    public EasyExcelWriterFactory(OutputStream outputStream) {
-//        excelWriter = EasyExcel.write(outputStream).build();
-//    }
-//
-//    public EasyExcelWriterFactory(File file) {
-//        excelWriter = EasyExcel.write(file).build();
-//    }
-//
-//    public EasyExcelWriterFactory(String filePath) {
-//        excelWriter = EasyExcel.write(filePath).build();
-//    }
-//
-//    /**
-//     * 链式模板表头写入
-//     *
-//     * @param headClazz 表头格式
-//     * @param data      数据 List<ExcelModel> 或者List<List<Object>>
-//     * @return
-//     */
-//    public EasyExcelWriterFactory writeModel(Class headClazz, List data, String sheetName) {
-//        excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(headClazz).build());
-//        return this;
-//    }
-//
-//    /**
-//     * 链式模板表头写入
-//     * @param obj 动态表头格式
-//     * @param data 数据 List<ExcelModel> 或者 List<List<Object>>
-//     * @param sheetName
-//     * @return
-//     */
-//    public EasyExcelWriterFactory writeModel(Object obj, List data, String sheetName) {
-//        excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(getObjectClass(obj)).build());
-//        return this;
-//
-//    }
-//
-//    private Class getObjectClass(Object object) {
-//        Class<?> objectClass = object.getClass();
-//        return objectClass;
-//    }
-//
-//    /**
-//     * 链式自定义表头写入
-//     * @param head 自定义表头格式
-//     * @param data 数据 List<ExcelModel> 或者List<List<Object>>
-//     * @param sheetName
-//     * @return
-//     */
-//    public EasyExcelWriterFactory write(List<List<String>> head, List data, String sheetName) {
-//        excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(head).build());
-//        return this;
-//    }
-//
-//    public void finish() {
-//        excelWriter.finish();
-//    }
-//
-//}
+package com.txz.mall.util.factory;
+
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+
+import java.io.File;
+import java.io.OutputStream;
+import java.util.List;
+
+/**
+ * @author: admin
+ * @date: 2022/10/28
+ * @time: 17:12
+ * @description: 自定义EasyExcel写工厂
+ */
+public class EasyExcelWriterFactory {
+    private int sheetNo = 0;
+    private ExcelWriter excelWriter = null;
+
+    public EasyExcelWriterFactory(OutputStream outputStream) {
+        excelWriter = EasyExcel.write(outputStream).build();
+    }
+
+    public EasyExcelWriterFactory(File file) {
+        excelWriter = EasyExcel.write(file).build();
+    }
+
+    public EasyExcelWriterFactory(String filePath) {
+        excelWriter = EasyExcel.write(filePath).build();
+    }
+
+    /**
+     * 链式模板表头写入
+     *
+     * @param headClazz 表头格式
+     * @param data      数据 List<ExcelModel> 或者List<List<Object>>
+     * @return
+     */
+    public EasyExcelWriterFactory writeModel(Class headClazz, List data, String sheetName) {
+        excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(headClazz).build());
+        return this;
+    }
+
+    /**
+     * 链式模板表头写入
+     * @param obj 动态表头格式
+     * @param data 数据 List<ExcelModel> 或者 List<List<Object>>
+     * @param sheetName
+     * @return
+     */
+    public EasyExcelWriterFactory writeModel(Object obj, List data, String sheetName) {
+        excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(getObjectClass(obj)).build());
+        return this;
+
+    }
+
+    private Class getObjectClass(Object object) {
+        Class<?> objectClass = object.getClass();
+        return objectClass;
+    }
+
+    /**
+     * 链式自定义表头写入
+     * @param head 自定义表头格式
+     * @param data 数据 List<ExcelModel> 或者List<List<Object>>
+     * @param sheetName
+     * @return
+     */
+    public EasyExcelWriterFactory write(List<List<String>> head, List data, String sheetName) {
+        excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(head).build());
+        return this;
+    }
+
+    public void finish() {
+        excelWriter.finish();
+    }
+
+}

+ 8 - 8
mall-service/src/test/resources/generator/template/controller-restful.ftl

@@ -1,4 +1,4 @@
-package ${basePackage}.web;
+package ${basePackage}.controller;
 
 import ${basePackage}.core.Result;
 import ${basePackage}.core.ResultGenerator;
@@ -24,25 +24,25 @@ public class ${modelNameUpperCamel}Controller {
     @PostMapping
     public Result add(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
         ${modelNameLowerCamel}Service.save(${modelNameLowerCamel});
-return Result.success();
+        return Result.success();
     }
 
     @DeleteMapping("/{id}")
-public Result delete(@PathVariable Long id) {
+    public Result delete(@PathVariable Long id) {
         ${modelNameLowerCamel}Service.deleteById(id);
-return Result.success();
+        return Result.success();
     }
 
     @PutMapping
     public Result update(@RequestBody ${modelNameUpperCamel} ${modelNameLowerCamel}) {
         ${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
-return Result.success();
+        return Result.success();
     }
 
     @GetMapping("/{id}")
-public Result detail(@PathVariable Long id) {
+    public Result detail(@PathVariable Long id) {
         ${modelNameUpperCamel} ${modelNameLowerCamel} = ${modelNameLowerCamel}Service.findById(id);
-return Result.success(${modelNameLowerCamel});
+        return Result.success(${modelNameLowerCamel});
     }
 
     @GetMapping
@@ -50,6 +50,6 @@ return Result.success(${modelNameLowerCamel});
         PageHelper.startPage(page, size);
         List<${modelNameUpperCamel}> list = ${modelNameLowerCamel}Service.findAll();
         PageInfo pageInfo = new PageInfo(list);
-return Result.success(pageInfo);
+        return Result.success(pageInfo);
     }
 }

+ 3 - 3
mall-service/src/test/resources/generator/template/controller.ftl

@@ -67,9 +67,9 @@ public class ${modelNameUpperCamel}Controller {
     		return Result.fail(ResultCode.USERID_IS_NULL);
     	}
     	try {
-${modelNameUpperCamel} ${modelNameLowerCamel} = new ${modelNameUpperCamel};
-${modelNameLowerCamel}.setIsDelete(1);
-${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
+			${modelNameUpperCamel} ${modelNameLowerCamel} = new ${modelNameUpperCamel};
+			${modelNameLowerCamel}.setIsDelete(1);
+			${modelNameLowerCamel}Service.update(${modelNameLowerCamel});
 		} catch (Exception e) {
 			log.error("删除对象操作异常e:{}",e);
 			return Result.fail(ResultCode.INTERNAL_SERVER_ERROR);