数据量较大时Excel导出工具类

pom引入部分(maven引入easyPoi也是可以的)

    <!-- AutoPoi Excel工具类-->
        <dependency>
            <groupId>org.jeecgframework</groupId>
            <artifactId>autopoi-web</artifactId>
            <version>1.3.9</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

代码部分,此处浏览器下载方法没有进行浏览器判断,可以自行添加。

package com.hightop.server.util;

import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author caixr
 * @title: ExcelUtils
 * @description: excel工具类-用于数据量较大的情况
 * @date 2022/4/7 9:03
 */
public class ExcelUtils {

    /**
     * pojo注解方式
     * 导出excel(浏览器端下载)
     *
     * @param params   excel参数
     * @param list     excel数据
     * @param classZ   注解类
     * @param response 响应头
     */
    public static <T> void exportExcelForExplorer(ExportParams params, List<T> list, Class<T> classZ, HttpServletResponse response) throws Exception {
        Workbook workbook = createExcel(params, list, classZ);
        String fileName = params.getTitle() + ".xlsx";
        response.setContentType("application/ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("gbk"), StandardCharsets.ISO_8859_1));
        OutputStream out = response.getOutputStream();
        workbook.write(out);
        out.flush();
        out.close();
    }

    /**
     * pojo注解方式
     * 导出excel(储存到服务器)
     *
     * @param params   excel参数
     * @param list     excel数据
     * @param classZ   注解类
     * @param filePath 响应头
     * @param fileName 文件名称
     */
    public static <T> void exportExcelForLocal(ExportParams params, List<T> list, Class<T> classZ, String filePath, String fileName) throws Exception {
        Workbook workbook = createExcel(params, list, classZ);
        File saveFile = new File(filePath);
        if (!saveFile.exists()) {
            saveFile.mkdirs();
        }
        fileName = fileName + ".xlsx";
        String name;
        Boolean flag = fileName.toLowerCase().endsWith("/");
        if(flag){
            name = filePath + fileName;
        }else{
            name = filePath + "/" +fileName;
        }
        FileOutputStream out = new FileOutputStream(name);
        workbook.write(out);
        out.close();
    }

    /**
     * pojo注解方式
     * 生成Excel
     *
     * @param params   excel参数
     * @param list     excel数据
     * @param classZ   注解类
     */
       private static <T> Workbook createExcel(ExportParams params, List<T> list, Class<T> classZ) {
        int totalPage = (list.size() / 10000) + 1;
        int pageSize = 10000;
        return ExcelExportUtil.exportBigExcel(params, classZ, (obj, page) -> {
            if (page > totalPage) {
                return null;
            }
            int fromIndex = (page - 1) * pageSize;
            int toIndex = page != totalPage ? fromIndex + pageSize : list.size();
            return new ArrayList<>(list.subList(fromIndex, toIndex));
        }, totalPage);
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容