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

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

    
        
            org.jeecgframework
            autopoi-web
            1.3.9
            
                
                    commons-codec
                    commons-codec
                
            
        

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

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  void exportExcelForExplorer(ExportParams params, List list, Class 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  void exportExcelForLocal(ExportParams params, List list, Class 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  Workbook createExcel(ExportParams params, List list, Class 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);
    }

}

你可能感兴趣的:(数据量较大时Excel导出工具类)