• 使用注解的方式导出excel数据


    使用注解进行导出步骤:
    系统导出 使用 ExcelUtils
    1:新建excel 包 、新建Excel 类 (例如 SysUserExcel 类)
    2:使用@Excel (注解由easyPoi 提供 相关字段解释参考官方文档,文档地址:http://easypoi.mydoc.io/#text_225967)
    3:查询数据 映射 SysUserExcel
    4:使用ExcelUtils 导出Excel
    excel工具类:ExcelUtils.java

    package com.demo.common.utils;
    
    import cn.afterturn.easypoi.excel.ExcelExportUtil;
    import cn.afterturn.easypoi.excel.entity.ExportParams;
    import cn.hutool.core.date.DateUtil;
    import org.apache.commons.lang3.StringUtils;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.springframework.beans.BeanUtils;
    
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.net.URLEncoder;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.List;
    
    /**
     * excel工具类
     *
     * @author system
     */
    public class ExcelUtils {
    
        /**
         * Excel导出
         *
         * @param response      response
         * @param fileName      文件名
         * @param list          数据List
         * @param pojoClass     对象Class
         */
        public static void exportExcel(HttpServletResponse response, String fileName, Collection<?> list,
                                         Class<?> pojoClass) throws IOException {
            if(StringUtils.isBlank(fileName)){
                //当前日期
                fileName = DateUtil.today();
            }else{
                fileName = fileName + DateUtil.today();
            }
    
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), pojoClass, list);
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8") + ".xls");
            ServletOutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
        }
    
        /**
         * Excel导出,先sourceList转换成List,再导出
         *
         * @param response      response
         * @param fileName      文件名
         * @param sourceList    原数据List
         * @param targetClass   目标对象Class
         */
        public static void exportExcelToTarget(HttpServletResponse response, String fileName, Collection<?> 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, targetList, targetClass);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    引入hutool依赖:
    5.7.22

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    导出时控制层代码demo:

        /**
         * 导出
         * @param params
         * @param response
         * @throws Exception
         */
        @PostMapping("/export")
        public void export(@RequestBody DemoDTO params, HttpServletResponse response) throws Exception {
            params.setPage(1);
            params.setLimit(999999999);
            PageData<DemoDTO> page = certRecordService.queryGovPage(params);
            List<DemoDTO> list = page.getList();
            ExcelUtils.exportExcelToTarget(response, null, list, DemoDTOExcel.class);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    工具类优先使用Hutool 文档参考地址:https://hutool.cn/docs/index.html#/

  • 相关阅读:
    【Java】split 分割方法
    NAS与SAN简介
    2022/9/17(cf·div2)https://codeforces.com/contest/1728
    【成像光敏描记图提取和处理】成像-光电容积描记-提取-脉搏率-估计(Matlab代码实现)
    C++ 练气期之一文看懂字符串
    罗克韦尔AB PLC RSLogix5000中创建新项目、任务、程序和例程的具体方法和步骤
    Vue模板语法的缩写是什么?
    Centos 7 Zabbix配置安装
    JVM 知识点全面梳理!
    Date类的学习笔记-超级详细
  • 原文地址:https://blog.csdn.net/upordown6263/article/details/127853743