欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

EasyExcel导出文件工具类

程序员文章站 2024-01-04 08:33:10
package com.benwunet.bks.utils;import java.util.Date;import com.alibaba.excel.EasyExcel;import com.alibaba.excel.ExcelWriter;import com.alibaba.excel.write.metadata.WriteSheet;import com.alibaba.fastjson.JSON;import org.apache.commons.collections4....
package com.benwunet.bks.utils;

import java.util.Date;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSON;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoyinchuan
 * @date 2020/11/23 0023 16:43
 */
@Component
@SuppressWarnings("ALL")
public class EasyExcelUtil {

    /**
     * @description web导出
     *
     * @params [response, list, fileNames, sheetName]
     * @return void
     * @author xiaoyinchuan
     * @date 2020/11/27 0027 11:42
     **/
    public void downloadFailedUsingJson(HttpServletResponse response, List<?> list, String fileNames, String sheetName) throws IOException {
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException();
        }
        if (StringUtils.isEmpty(fileNames)) {
            fileNames = new Date().toString();
        }
        if (StringUtils.isEmpty(sheetName)) {
            sheetName = "Sheet1";
        }
        // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode(fileNames, "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), list.get(0).getClass()).autoCloseStream(Boolean.FALSE).sheet(sheetName)
                    .doWrite(list);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<String, String>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }

    /**
     * @description 导出到本地
     *
     * @params [list, path, fileNames, sheetName]
     * @return void
     * @author xiaoyinchuan
     * @date 2020/11/27 0027 11:42
     **/
    public void downloadLocal(List<?> list, String path, String fileNames, String sheetName) {
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException();
        }
        if (StringUtils.isEmpty(fileNames)) {
            fileNames = new Date().toString();
        }
        if (StringUtils.isEmpty(sheetName)) {
            sheetName = "Sheet1";
        }
        ExcelWriter excelWriter = EasyExcel.write(path + fileNames + ".xlsx", list.get(0).getClass()).build();
        try {
            //标题
            WriteSheet test = EasyExcel.writerSheet(sheetName).build();
            //写入
            excelWriter.write(list, test);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            // 关闭
            excelWriter.finish();
        }
    }
}

 

本文地址:https://blog.csdn.net/qq_31452291/article/details/110226736

相关标签: excel java

上一篇:

下一篇: