Pārlūkot izejas kodu

文件导出字段翻译处理

lengfaqiang 2 gadi atpakaļ
vecāks
revīzija
9024917ae7

+ 29 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/model/enums/LogisticsOrderEnum.java

@@ -109,4 +109,33 @@ public enum LogisticsOrderEnum {
         }
         return codeList;
     }
+
+
+
+    public static String getDestination(String status) {
+        for (LogisticsOrderEnum logisticsOrderEnum : LogisticsOrderEnum.values()) {
+            if (logisticsOrderEnum.getStatus().equals(status)) {
+                return logisticsOrderEnum.getDestination();
+            }
+        }
+        return null;
+    }
+
+    public static LogisticsOrderEnum getLogisticsOrderEnumByCode(Integer code) {
+        for (LogisticsOrderEnum logisticsOrderEnum : LogisticsOrderEnum.values()) {
+            if (logisticsOrderEnum.getCode().equals(code)) {
+                return logisticsOrderEnum;
+            }
+        }
+        return null;
+    }
+
+    public static LogisticsOrderEnum getEntityByCode(Integer code) {
+        for (LogisticsOrderEnum logisticsOrderEnum : LogisticsOrderEnum.values()) {
+            if (logisticsOrderEnum.getCode().equals(code)) {
+                return logisticsOrderEnum;
+            }
+        }
+        return null;
+    }
 }

+ 50 - 0
sckw-common/sckw-common-core/src/main/java/com/sckw/core/model/enums/ProjectStatusEnum.java

@@ -0,0 +1,50 @@
+package com.sckw.core.model.enums;
+
+import lombok.Getter;
+
+import java.util.HashMap;
+import java.util.Map;
+
+@Getter
+public enum ProjectStatusEnum {
+    /**
+     *
+     */
+    SET_UP(0, "立项"),
+    RUNNING(1, "施工中"),
+    FINISH(2, "已竣工"),
+    ;
+    public static Map<Integer, ProjectStatusEnum> MAPS = new HashMap<>();
+
+    static {
+        for (ProjectStatusEnum e : values()) {
+            MAPS.put(e.getCode(), e);
+        }
+    }
+
+    private final Integer code;
+    private final String name;
+
+    ProjectStatusEnum(Integer code, String name) {
+        this.code = code;
+        this.name = name;
+    }
+
+    public static ProjectStatusEnum codeOf(Integer code) {
+        for (ProjectStatusEnum logisticsOrderEnum : ProjectStatusEnum.values()) {
+            if (logisticsOrderEnum.getCode().equals(code)) {
+                return logisticsOrderEnum;
+            }
+        }
+        return null;
+    }
+
+    public static ProjectStatusEnum getProjectStatusEnumByCode(Integer code) {
+        for (ProjectStatusEnum logisticsOrderEnum : ProjectStatusEnum.values()) {
+            if (logisticsOrderEnum.getCode().equals(code)) {
+                return logisticsOrderEnum;
+            }
+        }
+        return null;
+    }
+}

+ 35 - 0
sckw-common/sckw-common-excel/src/main/java/com/sckw/excel/annotation/EasyExcel.java

@@ -0,0 +1,35 @@
+package com.sckw.excel.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * @author lfdc
+ * @version v1
+ * @create 2020-11-28 15:37:35
+ * @copyright 指定枚举类注解
+ */
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface EasyExcel {
+    /**
+     * 控件类型
+     *
+     * @return
+     */
+    Class<? extends Enum> type();
+
+    /**
+     * 默认翻译字段类型
+     *
+     * @return
+     */
+    int status() default 0;
+
+    /**
+     * 是否选择翻译
+     *
+     * @return
+     */
+    boolean flag() default true;
+}

+ 120 - 0
sckw-common/sckw-common-excel/src/main/java/com/sckw/excel/config/easyexcel/ExcelConverter.java

@@ -0,0 +1,120 @@
+package com.sckw.excel.config.easyexcel;
+
+import com.alibaba.excel.converters.Converter;
+import com.alibaba.excel.enums.CellDataTypeEnum;
+import com.alibaba.excel.metadata.GlobalConfiguration;
+import com.alibaba.excel.metadata.data.WriteCellData;
+import com.alibaba.excel.metadata.property.ExcelContentProperty;
+import com.sckw.core.common.enums.NumberConstant;
+import com.sckw.excel.annotation.EasyExcel;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+/**
+ * @author lfdc
+ * @description 状态枚举信息获取(code->value,1代表失败 进行转换)
+ * @date 2023-07-28 10:07:51
+ */
+public class ExcelConverter implements Converter<String> {
+    @Override
+    public Class supportJavaTypeKey() {
+        //指定状态码类型
+        return String.class;
+    }
+
+    @Override
+    public CellDataTypeEnum supportExcelTypeKey() {
+        //指定需要获取的参数类型
+        return CellDataTypeEnum.STRING;
+    }
+
+    //导入时转换
+//    @Override
+//    public StatusEnum convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
+//        return null;
+//    }
+
+    /**
+     * 导出时转换
+     *
+     * @param value               状态码
+     * @param contentProperty     字段属性
+     * @param globalConfiguration 全局配置
+     * @return
+     * @throws Exception
+     */
+//    @Override
+//    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
+//        //value:状态码  contentProperty:字段属性  globalConfiguration:
+//        //获取字段属性中的注解
+//        Field field = contentProperty.getField();
+//        EasyExcel easyExcel = field.getAnnotation(EasyExcel.class);
+//        //获取注解中的枚举信息
+//        Class<? extends Enum> type = easyExcel.type();
+//        int status = easyExcel.status();
+//        Object invoke = null;
+//        if (NumberConstant.ONE == status) {
+//            //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
+//            Method codeOf = type.getMethod("getDestination", String.class);
+//            //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
+//            invoke = codeOf.invoke(type, value);
+//        } else if (NumberConstant.TWO == status) {
+//            //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
+//            Method codeOf = type.getMethod("getName", Integer.class);
+//            //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
+//            invoke = codeOf.invoke(type, value);
+//        } else {
+//            //获取枚举类的方法名 “codeOf”就是自己编写的函数,Integer.class 指定入参类型
+//            Method codeOf = type.getMethod("codeOf", Integer.class);
+//            //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
+//            invoke = codeOf.invoke(type, value);
+//            //枚举实例调用getname方法,得到name的值
+//            Method getName = invoke.getClass().getMethod("getName");
+//            //将转换的值进行返回
+//            invoke = String.valueOf(getName.invoke(invoke));
+//        }
+//        //将转换的值进行返回
+//        return new WriteCellData(invoke.toString());
+//    }
+    @Override
+    public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
+        //获取字段属性中的注解
+        Field field = contentProperty.getField();
+        EasyExcel easyExcel = field.getAnnotation(EasyExcel.class);
+        //获取注解中的枚举信息
+        Class<? extends Enum> type = easyExcel.type();
+        //获取枚举类的方法名 “codeOf”就是自己编写的函数,Integer.class 指定入参类型
+        int status = easyExcel.status();
+        boolean flag = easyExcel.flag();
+        Object invoke = null;
+        if (flag) {
+            if (NumberConstant.ONE == status) {
+                //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
+                Method codeOf = type.getMethod("getDestination", String.class);
+                //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
+                invoke = codeOf.invoke(type, value);
+            } else if (NumberConstant.TWO == status) {
+                //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
+                Method codeOf = type.getMethod("getName", Integer.class);
+                //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
+                invoke = codeOf.invoke(type, Integer.getInteger(value));
+            } else {
+                //获取枚举类的方法名 “codeOf”就是自己编写的函数,Integer.class 指定入参类型
+                Method codeOf = type.getMethod("getEntityByCode", Integer.class);
+                //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
+                invoke = codeOf.invoke(type, value);
+                //枚举实例调用getname方法,得到name的值
+                Method getName = invoke.getClass().getMethod("getDestination");
+                //将转换的值进行返回
+                invoke = String.valueOf(getName.invoke(invoke));
+            }
+        } else {
+            //将转换的值进行返回
+            return new WriteCellData(value.toString());
+        }
+        //将转换的值进行返回
+        return new WriteCellData(invoke.toString());
+    }
+
+}

+ 15 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/controller/ExcelExportController.java

@@ -9,6 +9,7 @@ import com.sckw.core.web.constant.HttpStatus;
 import com.sckw.core.web.response.HttpResult;
 import com.sckw.example.dao.KwsDeptDao;
 import com.sckw.example.model.KwsDept;
+import com.sckw.example.model.vo.ProjectPageVo;
 import com.sckw.example.model.vo.SysUserVo;
 import com.sckw.example.service.ExcelExportService;
 import com.sckw.excel.easyexcel.ExcelImportListener;
@@ -189,4 +190,18 @@ public class ExcelExportController {
             throw new RuntimeException(e);
         }
     }
+
+    /**
+     * 导出注解翻译示例
+     */
+    @RequestMapping(value = "/easyExcelDemo", method = RequestMethod.GET)
+    public HttpResult easyExcelDemo() {
+        HttpServletResponse response = RequestHolder.getResponse();
+        List<ProjectPageVo> list = excelExportService.easyExcelDemo();
+        if (!CollectionUtils.isEmpty(list)) {
+            ExcelUtil.download(response, ProjectPageVo.class, list);
+            return null;
+        }
+        return HttpResult.error("没有可导出的数据");
+    }
 }

+ 32 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/model/vo/ProjectPageVo.java

@@ -0,0 +1,32 @@
+package com.sckw.example.model.vo;
+
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.sckw.core.common.enums.NumberConstant;
+import com.sckw.core.model.enums.ProjectStatusEnum;
+import com.sckw.excel.annotation.EasyExcel;
+import com.sckw.excel.annotation.ExcelContext;
+import com.sckw.excel.config.easyexcel.ExcelConverter;
+import lombok.Data;
+
+/**
+ * @author lfdc
+ * @description
+ * @date 2023-07-28 11:07:15
+ */
+@Data
+@ExcelContext(fileName = "企业列表", sheetName = "企业列表")
+public class ProjectPageVo {
+    /**
+     * 项目状态 ,index = 0
+     */
+    @ExcelProperty(value = "项目状态", converter = ExcelConverter.class)
+    @EasyExcel(type = ProjectStatusEnum.class, status = NumberConstant.ONE)
+    private Integer status;
+
+    /**
+     * 项目备注,index = 0
+     */
+    @ExcelProperty(value = "项目备注")
+    private String remark;
+
+}

+ 15 - 0
sckw-modules/sckw-example/src/main/java/com/sckw/example/service/ExcelExportService.java

@@ -5,6 +5,7 @@ import com.alibaba.excel.ExcelReader;
 import com.alibaba.excel.read.metadata.ReadSheet;
 import com.sckw.core.web.constant.HttpStatus;
 import com.sckw.core.web.response.HttpResult;
+import com.sckw.example.model.vo.ProjectPageVo;
 import com.sckw.example.model.vo.SysEnterpriseVo;
 import com.sckw.example.model.vo.SysUserVo;
 import com.sckw.excel.common.FileType;
@@ -15,6 +16,7 @@ import jakarta.servlet.http.HttpServletResponse;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
+
 import java.io.*;
 import java.net.URLEncoder;
 import java.util.ArrayList;
@@ -208,4 +210,17 @@ public class ExcelExportService {
         }
         return HttpResult.ok(HttpStatus.SUCCESS_CODE);
     }
+
+    public List<ProjectPageVo> easyExcelDemo() {
+        ProjectPageVo projectPageVo = new ProjectPageVo();
+        projectPageVo.setStatus(0);
+        projectPageVo.setRemark("0");
+        ProjectPageVo projectPageVo1 = new ProjectPageVo();
+        projectPageVo1.setStatus(1);
+        projectPageVo1.setRemark("0");
+        List<ProjectPageVo> list = new ArrayList<>();
+        list.add(projectPageVo);
+        list.add(projectPageVo1);
+        return list;
+    }
 }

+ 41 - 0
sckw-modules/sckw-report/src/main/java/com/sckw/report/service/vo/AcceptCarriageOrderExcelVo.java

@@ -1,5 +1,7 @@
 package com.sckw.report.service.vo;
 
+import com.alibaba.excel.annotation.ExcelProperty;
+import com.sckw.excel.annotation.ExcelContext;
 import lombok.Data;
 
 /**
@@ -8,66 +10,82 @@ import lombok.Data;
  * @date 2023-07-24 13:07:44
  */
 @Data
+@ExcelContext(fileName = "承运订单列表", sheetName = "承运订单列表")
 public class AcceptCarriageOrderExcelVo {
     /**
      * 结算周期
      */
+    @ExcelProperty(value = "结算周期")
     private String accountsCycle;
     /**
      * 发起人
      */
+    @ExcelProperty(value = "发起人")
     private String appointor;
     /**
      * 计费方式
      */
+    @ExcelProperty(value = "计费方式")
     private String billingMode;
     /**
      * 承运单位
      */
+    @ExcelProperty(value = "承运单位")
     private String carrierCompany;
     /**
      * 承运联系人
      */
+    @ExcelProperty(value = "承运联系人")
     private String carrierContacts;
     /**
      * 承运订单
      */
+    @ExcelProperty(value = "承运订单")
     private String carrierOrder;
     /**
      * 承运联系电话
      */
+    @ExcelProperty(value = "承运联系电话")
     private String carrierPhone;
     /**
      * 分配承运量
      */
+    @ExcelProperty(value = "分配承运量")
     private String carryingCapacity;
     /**
      * 托运单位
      */
+    @ExcelProperty(value = "托运单位")
     private String consignCompany;
     /**
      * 托运联系人
      */
+    @ExcelProperty(value = "托运联系人")
     private String consignContacts;
     /**
      * 托运联系电话
      */
+    @ExcelProperty(value = "托运联系电话")
     private String consignPhone;
     /**
      * 合同
      */
+    @ExcelProperty(value = "合同")
     private String contract;
     /**
      * 扣亏货值/元/吨
      */
+    @ExcelProperty(value = "扣亏货值/元/吨")
     private String deficitAmount;
     /**
      * 货物名称
      */
+    @ExcelProperty(value = "货物名称")
     private String goodsName;
     /**
      * 装货地点
      */
+    @ExcelProperty(value = "装货地点")
     private String loadAddress;
     /**
      * 数据库id
@@ -76,64 +94,87 @@ public class AcceptCarriageOrderExcelVo {
     /**
      * 托运订单号
      */
+    @ExcelProperty(value = "托运订单号")
     private String lOrderNo;
     /**
      * 合理损耗
      */
+    @ExcelProperty(value = "合理损耗")
     private String loss;
     /**
      * 计划时间
      */
+    @ExcelProperty(value = "计划时间")
     private String plannedDateTime;
     /**
      * 运价
      */
+    @ExcelProperty(value = "运价")
     private String price;
     /**
      * 收货联系人
      */
+    @ExcelProperty(value = "收货联系人")
     private String receiveGoodsContacts;
     /**
      * 计划收货时间
      */
+    @ExcelProperty(value = "计划收货时间")
     private String receiveGoodsDateTime;
     /**
      * 收货电话
      */
+    @ExcelProperty(value = "收货电话")
     private String receiveGoodsPhone;
     /**
      * 备注
      */
+    @ExcelProperty(value = "备注")
     private String remark;
     /**
      * 发货联系人
      */
+    @ExcelProperty(value = "发货联系人")
     private String shipmentsContacts;
     /**
      * 计划发货时间
      */
+    @ExcelProperty(value = "计划发货时间")
     private String shipmentsDateTime;
     /**
      * 发货电话
      */
+    @ExcelProperty(value = "发货电话")
     private String shipmentsPhone;
     /**
      * 签约方式
      */
+    @ExcelProperty(value = "签约方式")
     private String signing;
     /**
      * 状态
      */
+    @ExcelProperty(value = "状态")
     private String status;
     /**
      * 卸货地点
      */
+    @ExcelProperty(value = "卸货地点")
     private String unloadAddress;
     /**
      * 关联承运订单号
      */
+    @ExcelProperty(value = "关联承运订单号")
     private String wOrderNo;
+    /**
+     * 创建时间
+     */
+    @ExcelProperty(value = "创建时间")
     private String createTime;
+    /**
+     * 修改时间
+     */
+    @ExcelProperty(value = "修改时间")
     private String updateTime;
 
 }

+ 43 - 6
sckw-modules/sckw-report/src/main/java/com/sckw/report/service/vo/ConsignOrderExcelVo.java

@@ -1,7 +1,11 @@
 package com.sckw.report.service.vo;
 
 import com.alibaba.excel.annotation.ExcelProperty;
+import com.sckw.core.common.enums.NumberConstant;
+import com.sckw.core.model.enums.LogisticsOrderEnum;
+import com.sckw.excel.annotation.EasyExcel;
 import com.sckw.excel.annotation.ExcelContext;
+import com.sckw.excel.config.easyexcel.ExcelConverter;
 import lombok.Data;
 
 import java.io.Serializable;
@@ -17,68 +21,77 @@ public class ConsignOrderExcelVo implements Serializable {
     /**
      * 结算周期
      */
-    @ExcelProperty(value = "结算周期")
+//    @ExcelProperty(value = "结算周期")
     private String accountsCycle;
     /**
      * 发起人
      */
-    @ExcelProperty(value = "发起人")
+    //    @ExcelProperty(value = "发起人")
     private String appointor;
     /**
      * 计费方式
      */
-    @ExcelProperty(value = "计费方式")
+    //     @ExcelProperty(value = "计费方式")
     private String billingMode;
     /**
      * 承运单位
      */
-    @ExcelProperty(value = "承运单位")
+    //    @ExcelProperty(value = "承运单位")
     private String carrierCompany;
     /**
      * 承运联系人
      */
-    @ExcelProperty(value = "承运联系人")
+    //   @ExcelProperty(value = "承运联系人")
     private String carrierContacts;
     /**
      * 承运订单
      */
-    @ExcelProperty(value = "承运订单")
+    //    @ExcelProperty(value = "承运订单")
     private String carrierOrder;
     /**
      * 承运联系电话
      */
+    //    @ExcelProperty(value = "承运联系电话")
     private String carrierPhone;
     /**
      * 分配承运量
      */
+    //   @ExcelProperty(value = "分配承运量")
     private String carryingCapacity;
     /**
      * 托运单位
      */
+    //   @ExcelProperty(value = "托运单位")
     private String consignCompany;
     /**
      * 托运联系人
      */
+    //   @ExcelProperty(value = "托运联系人")
     private String consignContacts;
     /**
      * 托运联系电话
      */
+    //    @ExcelProperty(value = "托运联系电话")
     private String consignPhone;
     /**
      * 合同
      */
+    //   @ExcelProperty(value = "合同")
     private String contract;
     /**
      * 扣亏货值/元/吨
      */
+    //    @ExcelProperty(value = "扣亏货值/元/吨")
     private String deficitAmount;
     /**
      * 货物名称
      */
+    //    @ExcelProperty(value = "货物名称")
     private String goodsName;
     /**
      * 装货地点
      */
+//    @ExcelProperty(value = "装货地点")
     private String loadAddress;
     /**
      * 数据库id
@@ -87,63 +100,87 @@ public class ConsignOrderExcelVo implements Serializable {
     /**
      * 托运订单号
      */
+//    @ExcelProperty(value = "托运订单号")
     private String lOrderNo;
     /**
      * 合理损耗
      */
+    //    @ExcelProperty(value = "合理损耗")
     private String loss;
     /**
      * 计划时间
      */
+    //   @ExcelProperty(value = "计划时间")
     private String plannedDateTime;
     /**
      * 运价
      */
+    //    @ExcelProperty(value = "运价")
     private String price;
     /**
      * 收货联系人
      */
+    //   @ExcelProperty(value = "收货联系人")
     private String receiveGoodsContacts;
     /**
      * 计划收货时间
      */
+    //   @ExcelProperty(value = "计划收货时间")
     private String receiveGoodsDateTime;
     /**
      * 收货电话
      */
+    //     @ExcelProperty(value = "收货电话")
     private String receiveGoodsPhone;
     /**
      * 备注
      */
+    //     @ExcelProperty(value = "备注")
     private String remark;
     /**
      * 发货联系人
      */
+    //    @ExcelProperty(value = "发货联系人")
     private String shipmentsContacts;
     /**
      * 计划发货时间
      */
+    //    @ExcelProperty(value = "计划发货时间")
     private String shipmentsDateTime;
     /**
      * 发货电话
      */
+    //    @ExcelProperty(value = "发货电话")
     private String shipmentsPhone;
     /**
      * 签约方式
      */
+    //    @ExcelProperty(value = "签约方式")
     private String signing;
     /**
      * 状态
      */
+    @EasyExcel(type = LogisticsOrderEnum.class, status = NumberConstant.ONE, flag = false)
+    @ExcelProperty(value = "状态", converter = ExcelConverter.class)
     private String status;
     /**
      * 卸货地点
      */
+    //    @ExcelProperty(value = "卸货地点")
     private String unloadAddress;
     /**
      * 关联承运订单号
      */
+    //    @ExcelProperty(value = "关联承运订单号")
     private String wOrderNo;
+    /**
+     * 创建时间
+     */
+    //   @ExcelProperty(value = "创建时间")
     private String createTime;
+    /**
+     * 修改时间
+     */
+    //    @ExcelProperty(value = "修改时间")
     private String updateTime;
 }