EasyPOI导出动态表头

PO


import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.handler.inter.IExcelDataModel;
import cn.afterturn.easypoi.handler.inter.IExcelModel;
import lombok.Data;

import javax.validation.constraints.NotNull;

@Data
public class BaseMaterialProductExcel implements IExcelDataModel, IExcelModel {


    /** 名称 */
    @NotNull(message = "名称不能为空")
    @Excel(name = "名称",width=20D)
    private String materialName;

    /** 规格型号 */
    // 规格型号 字典:jdz_prodect_spec
    @NotNull(message = "规格型号")
    @Excel(name = "规格型号",width=20D)
    private String specification;

    /** 单位 */
    //单位 字典:jdz_product_unit
    @NotNull(message = "单位不能为空")
    @Excel(name = "单位",width=20D)
    private String unit;





    private String erroMsg;

    private Integer rowNum;

    @Override
    public String getErrorMsg() {
        return this.erroMsg;
    }

    @Override
    public void setErrorMsg(String errorMsg) {
        this.erroMsg = errorMsg;
    }

    @Override
    public Integer getRowNum() {
        return this.rowNum;
    }

    @Override
    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }


}

controller

 /**
     * 查询泡豆单导出
     */
    @GetMapping("/importMaterialProductList")
    public Result importMaterialProductList(@RequestParam("file") final MultipartFile file) throws IOException {
        String msg = baseMaterialProductService.importMaterialProductList(file.getInputStream(), getCompanyId());
        return ResultGenerator.getSuccessResult(msg);
    }


    /**
     * 主产品导出
     */
    @GetMapping("/exportMaterialProductList")
    public void exportMaterialProductList(BaseMaterialProduct materialBillInfo, HttpServletResponse response) {
       //需要导出的数据
        List<BaseMaterialProductExcel> pageInfo = baseMaterialProductService.exportMaterialProductList(materialBillInfo);
        // 设置表头配置,重点
        BaseMaterialProductExcel materialProdExceBeanClass = baseMaterialProductService.getMaterialProdExceBeanClass(getCompanyId());

        final Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "产成品", ExcelType.XSSF),
                materialProdExceBeanClass.getClass(), pageInfo);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("产成品"+LocalDateTime.now() , "UTF-8")+".xls");

            workbook.write(out);
            out.flush();
        } catch (final Exception e) {

        } finally {
            try {
                out.close();
            } catch (final IOException e) {
            }
        }
    }

baseMaterialProductService


    @Override
    public BaseMaterialProductExcel getMaterialProdExceBeanClass(Integer companyId) {

        List<IotDictionary> jdz_product_unit = new LinkedList<>();
        List<IotDictionary> jdz_prodect_spec = new LinkedList<>();

        // 单位
        jdz_product_unit = dictionaryMapper.selectDictionaryByDictType(companyId,"jdz_product_unit");
        // 规格
        jdz_prodect_spec = dictionaryMapper.selectDictionaryByDictType(companyId,"jdz_prodect_spec");

        // 封装动态表头
        StringBuffer prodUnit = new StringBuffer("单位[");
        jdz_product_unit.stream().forEach(e->prodUnit.append(",").append(e.getDicName()));
        prodUnit.append("]");
        String unit = prodUnit.toString().replaceFirst(",", "");

        StringBuffer prodect_spec = new StringBuffer("规格型号[");
        jdz_prodect_spec.stream().forEach(e->prodect_spec.append(",").append(e.getDicName()));
        prodect_spec.append("]");
        String spec = prodect_spec.toString().replaceFirst(",", "");

        // 替换easyPoi @Excel 的name值
        BaseMaterialProductExcel excel = new BaseMaterialProductExcel();
        IotAnnotationUtils.changeAnnotationValue("specification",excel.getClass(), Excel.class,"name",spec);
        IotAnnotationUtils.changeAnnotationValue("unit",excel.getClass(), Excel.class,"name",unit);

        return excel;
    }

IotAnnotationUtils


import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.Map;

public class IotAnnotationUtils {
    /**
     * 变更注解的属性值
     *
     * @param variableName  属性名称
     * @param clazz     注解所在的实体类
     * @param tClass    注解类
     * @param filedName 要修改的注解属性名
     * @param value     要设置的属性值
     */
    public static <A extends Annotation> Class<?> changeAnnotationValue(String variableName, Class<?> clazz, Class<A> tClass, String filedName, Object value) {
        try {
            // 返回所有的属性
            Field field = clazz.getDeclaredField(variableName);
            A annotation = field.getAnnotation(tClass);
            setAnnotationValue(annotation, filedName, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return clazz;
    }

    /**
     * 设置注解中的字段值
     *
     * @param annotation   要修改的注解实例
     * @param fieldName    要修改的注解属性名
     * @param value        要设置的属性值
     */
    public static void setAnnotationValue( Annotation annotation, String fieldName, Object value) throws NoSuchFieldException, IllegalAccessException {
        InvocationHandler handler = Proxy.getInvocationHandler(annotation);
        Field field = handler.getClass().getDeclaredField("memberValues");
        //允许访问私有变量
        field.setAccessible(true);
        // 获取 memberValues
        Map memberValues = (Map) field.get(handler);
        // 修改 value 属性值
        memberValues.put(fieldName, value);
    }
}

相关推荐

  1. EasyPOI导出动态表头

    2024-04-05 15:30:04       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 15:30:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 15:30:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 15:30:04       82 阅读
  4. Python语言-面向对象

    2024-04-05 15:30:04       91 阅读

热门阅读

  1. Springboot集成hanlp快速入门demo

    2024-04-05 15:30:04       42 阅读
  2. 题目 1752: 对称矩阵

    2024-04-05 15:30:04       34 阅读
  3. Spring Boot集成Spring Retry快速入门Demo

    2024-04-05 15:30:04       36 阅读
  4. 深入理解nginx mp4流媒体模块[下下]

    2024-04-05 15:30:04       38 阅读
  5. Day.21

    2024-04-05 15:30:04       40 阅读
  6. 教你如何在 WebView 中实现优雅的后退键处理

    2024-04-05 15:30:04       30 阅读
  7. C# 委托与事件 深入

    2024-04-05 15:30:04       30 阅读
  8. 金融科技包含领域

    2024-04-05 15:30:04       32 阅读
  9. [环境配置]conda 64位安装32位python

    2024-04-05 15:30:04       35 阅读
  10. LeetCode的使用方法

    2024-04-05 15:30:04       29 阅读
  11. 初学者如何入门深度学习?

    2024-04-05 15:30:04       46 阅读
  12. TypeScript:泛型

    2024-04-05 15:30:04       41 阅读
  13. SSH数据加密传输:安全连接新体验

    2024-04-05 15:30:04       33 阅读
  14. android 扫描二维码

    2024-04-05 15:30:04       35 阅读