首页 > 其他分享 >easyExcel导出单个文件多个sheet页-注解方式

easyExcel导出单个文件多个sheet页-注解方式

时间:2023-04-21 10:45:16浏览次数:37  
标签:sheet String ColumnWidth easyExcel ExcelProperty private THIN 注解 BorderStyle

easyExcel注解方式导出,为什么要用注解方式?

注解方式更简单、更方便,相比非注解的方式减少了大量代码。

但是,注解方式不够灵活,精确度也不高,比如列宽,注解方式仅支持int类型,最大255个字符,而非注解方式就要精确的多,可以随意设置。

所以,对于要求比较高的、复杂的Excel,还是推荐使用非注解的方式实现。

重点一:注解方式实现复杂表头

表头样式(网上找的,非原创)

在这里插入图片描述

表实现类(网上找的,该代码非原创)

@Data
public class WmsOperationQueryExcleVO {

    //时间段
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","时间段/项目","时间段/项目"},index = 0)
    private String timeSlot;

    //包裹签收/个
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","前端","包裹签收/个"},index = 1)
    private String packageSigning;

    //收货完成/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","前端","收货完成/件"},index = 2)
    private String receivingCompleted;

    //上架完成/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","前端","上架完成/件"},index = 3)
    private String completedShelf;

    //调拔入库/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","前端","调拔入库/件"},index = 4)
    private String transferToStorage;

    //退货入库/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","前端","退货入库/件"},index = 5)
    private String returnWarehousing;

    //其他入库/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","前端","其他入库/件"},index = 6)
    private String otherWarehousing;

    //订单拣货完成/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","中端","订单拣货完成/件"},index = 7)
    private String orderPickingCompleted;

    //海外仓拣货完成/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","中端","海外仓拣货完成/件"},index = 8)
    private String overseasWarehousePicking;

    //FBA拣货完成/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","中端","FBA拣货完成/件"},index = 9)
    private String pickingFba;

    //其他出库/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","中端","其他出库/件"},index = 10)
    private String otherStockOut;

    //订单分拣完成/件
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","后端","订单分拣完成/件"},index = 11)
    private String orderSortingCompleted;

    //订单包装完成/单
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","后端","订单包装完成/单"},index = 12)
    private String orderPackaging;

    //海外仓包装完成/单
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","后端","海外仓包装完成/单"},index = 13)
    private String overseasWarehousePackaging;

    //FBA包装完成/单
    @ColumnWidth(14)
    @ExcelProperty(value = {"仓库运营报表(按天查询)","后端","FBA包装完成/单"},index = 14)
    private String packagingFba;
}

重点二:多个sheet页的Excel文件导出(直接上代码)

pom.xlm引入maven

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>easyexcel</artifactId>
			<version>2.2.6</version>
		</dependency>

ExcelUtils.java导出工具类,具体导出实现

/**
 * excel工具类
 */
public class ExcelUtils {

    /**
     * 通用 Excel导出
     *
     * @param response      response
     * @param fileName      文件名
     * @param sheetName     sheetName
     * @param list          数据List
     * @param pojoClass     对象Class
     */
    public static void exportExcel(HttpServletResponse response, String fileName, String sheetName, List<?> list,
                                     Class<?> pojoClass) throws IOException {
        if(StringUtils.isBlank(fileName)){
            //当前日期
            fileName = DateUtils.format(new Date());
        }

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");//不设置该参数前端(vue)接收不到文件名
        response.setHeader("Content-disposition", "attachment;filename=" +  fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), pojoClass).sheet(sheetName).doWrite(list);
    }

    /**
     * Excel导出,先sourceList转换成List<targetClass>,再导出
     * 单个sheet
     *
     * @param response      response
     * @param fileName      文件名
     * @param sheetName     sheetName
     * @param sourceList    原数据List
     * @param targetClass   目标对象Class
     */
    public static void exportExcelToTarget(HttpServletResponse response, String fileName, String sheetName, List<?> sourceList,
                                     Class<?> targetClass) throws Exception {
        List targetList = new ArrayList<>(sourceList.size());
        for(Object source : sourceList){
            Object target = targetClass.newInstance();
            BeanUtils.copyProperties(source, target);
            targetList.add(target);
        }

        exportExcel(response, fileName, sheetName, targetList, targetClass);
    }

    /**
     * Excel导出,先sourceList转换成List<targetClass>,再导出
     * 多个sheet
     *
     * @param response      response
     * @param fileName      文件名
     * @param sheetNames     sheetName列表
     * @param sourceMap    原数据Map,key为sheetName,value为list
     * @param targetClass   目标对象Class
     */
    public static void exportExcel(HttpServletResponse response, String fileName, List<String> sheetNames, Map<String, Object> sourceMap,
                                           Class<?> targetClass) throws Exception {
        if(StringUtils.isBlank(fileName)){
            //当前日期
            fileName = DateUtils.format(new Date());
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");//不设置该参数前端(vue)接收不到文件名
        response.setHeader("Content-disposition", "attachment;filename=" +  fileName + ".xlsx");

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), targetClass).build();
        for (int i = 0; i < sheetNames.size(); i++) {
            List list = (List) sourceMap.get(sheetNames.get(i));
            List targetList = new ArrayList<>(list.size());
            for(Object source : list){
                Object target = targetClass.newInstance();
                BeanUtils.copyProperties(source, target);
                targetList.add(target);
            }
            WriteSheet writeSheet = EasyExcel.writerSheet(i, sheetNames.get(i)).build();
            excelWriter.write(targetList, writeSheet);
        }
        excelWriter.finish();
    }

}


参数类DTO

@Data
public class ExportDataParamDTO {
    /**
     * 文件名
     */
    private String fileName;
    /**
     * 标签页名称集合
     */
    private List<String> sheetNames;
    /**
     * 数据map,key标签页名称,value数据list
     */
    private Map<String, Object> dataMap;
    /**
     * 目标类型
     */
    private Class<?> targetClass;
}

导出实体类,具体样式在该类中注解方式实现

/**
 * 导出excel表实体类
 *
 * @author Mark [email protected]
 * @since 1.0.0 2022-11-21
 */
@Data
@HeadRowHeight(42)
//@ContentRowHeight(14)
@HeadStyle(fillBackgroundColor= 55, horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
@HeadFontStyle(fontName = "宋体", fontHeightInPoints = 9, bold = true)
@ContentFontStyle(fontName = "宋体", fontHeightInPoints = 8, bold = false)
public class ExportExcel {

    @ExcelProperty("期数")
    @ColumnWidth(4)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private Integer qici;

    @ExcelProperty("有效期数")
    @ColumnWidth(4)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String yxq; //有效期次

    @ExcelProperty("高度(m)")
    @ColumnWidth(6)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private Double PRELOADH;

    @ExcelProperty("观测")
    @ColumnWidth(26)
    @ContentStyle(borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String workinfoname;

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ExcelProperty("查看时间")
    @ColumnWidth(15)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private Date mtimeW;

    @ExcelProperty("成果")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String mavalue;

    @ExcelProperty("修改值")
    @ColumnWidth(7)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String cvalue;

    @ExcelProperty("状态")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String pstate;

    @ExcelProperty("天数")
    @ColumnWidth(4)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private Integer day;

    @ExcelProperty("间隔(天)")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private Integer dayJg;

    @ExcelProperty("当前值")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String mavalueBc;

    @ExcelProperty("累计值")
    @ColumnWidth(8)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String mavalueLj;

    @ExcelProperty("速率")
    @ColumnWidth(4)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String mavalueSl;

    @DateTimeFormat("yyyy-MM-dd HH:mm:ss")
    @ExcelProperty("上传时间")
    @ColumnWidth(15)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private Date createdate;

    @ExcelProperty("人员名称")
    @ColumnWidth(6)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String sname;

    @ExcelProperty("删除状态")
    @ColumnWidth(4)
    @ContentStyle(horizontalAlignment = HorizontalAlignment.CENTER, borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String useflag;

    @ExcelProperty("备注")
    @ColumnWidth(11)
    @ContentStyle(borderLeft = BorderStyle.THIN, borderRight = BorderStyle.THIN, borderTop = BorderStyle.THIN, borderBottom = BorderStyle.THIN)
    private String remark;

}

Controller实现方法

@GetMapping("exportResultList")
    @ApiOperation("导出")
    @LogOperation("导出")
    public void export(String ids, HttpServletResponse response) throws Exception {
        String[] split = ids.split(",");
        //数据
        ExportDataParamDTO resultData = checkpointService.exportResultList(Arrays.asList(split));
        //导出操作
        ExcelUtils.exportExcel(response, resultData.getFileName(), resultData.getSheetNames(), resultData.getDataMap(), resultData.getTargetClass());
    }

Excel

完美!

标签:sheet,String,ColumnWidth,easyExcel,ExcelProperty,private,THIN,注解,BorderStyle
From: https://www.cnblogs.com/Dog1363786601/p/17339512.html

相关文章

  • 记录一次使用 表达式引擎 自定义注解 还有 sql union all 实现对数据库数据提取、重组
    这样编写减少了前后端很多没必要的遍历,以及if判断并最大限度提高了代码的可变通性额外需要学习的是ORM框架下,如何接收多表(各表结构不同)操作后,sql返回的新结构的临时表问题表达式引擎用到的依赖<dependency><groupId>org.apache.commons</groupId>......
  • springboot 事件监听@EventListener注解用法
    前言关于@EventListener注解,百度了一下,网上的教程很多都是继承这个,实现那个的,其实根本用不着这么麻烦,所以就写了此文,如文章所讲有误,还请谅解更多详细用法请百度一下~作用关于事件监听,目前我的用法最多的就是记录日志之类的。在此之前我们记录日志一般都是先把日志的service注......
  • 自定义注解+AOP实现参数校验
          转: https://www.cnblogs.com/mcj123/p/16842043.htmlhttps://www.cnblogs.com/fps2tao/p/13921207.html https://www.cnblogs.com/fps2tao/p/13921106.htmlhttps://www.cnblogs.com/fps2tao/p/13306246.html ......
  • kubectl apply -f --record 是否将当前创建对象创建命令保存到Annotation注解中中。
    kubectlcreate-ftomcat-app1.yaml--save-config--recordkubectlapply-ftomcat-app1.yaml--record#推荐命令 --record  #是否将当前对象创建命令保存至Annotation中,布尔型数据(true或false) --save-config  #是否将当前对象配置信息保存至......
  • 6.自定义注解与设计模式
    自定义注解与设计模式课程目标熟悉注解底层实现原理完成ORM框架底层原理常用设计模式单例、工厂、代理一.自定义注解1.1什么是注解?Jdk1.5新增新技术,注解。很多框架为了简化代码,都会提供有些注解。可以理解为插件,是代码级别的插件,在类的方法上写:@XXX,就是在......
  • 插入式注解
    目录0x01Lombok0x02插入式注解处理器AbstractProcessor注解配置参数工具类0x03初始化注解处理器JavaCompilerJavacProcessingEnvironmentJavacProcessingEnvironment#DiscoveredProcessorsJavacProcessingEnvironment#ProcessorState0x04运行注解处理器JavaCompilerJavacProcess......
  • pringboot之restfull接口规范注解(二)
    1,springboot逆向mybatis生成接口类2,执行generator生成接口类1,控制台使用mvn命令:2,双击mvn里面的pulgins插件下的renerator启动插件3,创建一个控制器4,application配置文件添加引入mapper的xml路径5,执行效果6,测试代码地址本文永久更新地址:1,springboot逆向mybatis......
  • Java 4种校验注解(值校验、范围校验、长度校验、格式校验)
    1Maven依赖<!--第一种方式导入校验依赖--><dependency><groupId>javax.validation</groupId><artifactId>validation-api</artifactId><version>2.0.1.Final</version></dependency><!--第二种方式导入校验......
  • Spring AOP官方文档学习笔记(二)之基于注解的Spring AOP
    1.@Aspect注解(1)@Aspect注解用于声明一个切面类,我们可在该类中来自定义切面,早在Spring之前,AspectJ框架中就已经存在了这么一个注解,而Spring为了提供统一的注解风格,因此采用了和AspectJ框架相同的注解方式,这便是@Aspect注解的由来,换句话说,在Spring想做AOP框架之前,AspectJAOP框......
  • SpingROOT注解
    @TableName("CG_WO_FAULT_EXPERT_PAPERS")实体类加这个注解 controller直接可以去查询表了@Accessors(chain=true)注解是用来干嘛的?这个注解是来自与Lombok里的,具体的作用是开启链式编程,让我们写代码更加方便。.排查 经过排查发现是因为 @ApiModel 直接使用不规范导......