第一步:
导入依赖

    <!-- 导处Excel -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>3.0.0-beta3</version>
    </dependency>

第二步:
新建方法类

package com.lc.yangzi.module.marketing.sell;

import lombok.extern.slf4j.Slf4j;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.poi.util.IOUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * Excel工具类
 */
@Slf4j
public class ExcelUtils {

    /**
     * 导出Excel(07版.xlsx)到指定路径下
     *
     * @param path      路径
     * @param excelName Excel名称
     * @param sheetName sheet页名称
     * @param clazz     Excel要转换的类型
     * @param data      要导出的数据
     */
    public static void export2File(String path, String excelName, String sheetName, Class clazz, List data) {
        String fileName = path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue());
        EasyExcel.write(fileName, clazz).sheet(sheetName).doWrite(data);
    }

    /**
     * 导出Excel(07版.xlsx)到web
     *
     * @param response  响应
     * @param excelName Excel名称
     * @param sheetName sheet页名称
     * @param clazz     Excel要转换的类型
     * @param data      要导出的数据
     * @throws Exception
     */
    public static void export2Web(HttpServletResponse response, String excelName, String sheetName, Class clazz, List data) throws Exception {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        excelName = URLEncoder.encode(excelName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());
        EasyExcel.write(response.getOutputStream(), clazz).sheet(sheetName).doWrite(data);
    }

    /**
     * 将指定位置指定名称的Excel导出到web
     *
     * @param response  响应
     * @param path      文件路径
     * @param excelName 文件名称
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String export2Web4File(HttpServletResponse response, String path, String excelName) throws UnsupportedEncodingException {
        File file = new File(path.concat(excelName).concat(ExcelTypeEnum.XLSX.getValue()));
        if (!file.exists()) {
            return "文件不存在!";
        }

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        excelName = URLEncoder.encode(excelName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + excelName + ExcelTypeEnum.XLSX.getValue());

        try (
                FileInputStream in = new FileInputStream(file);
                ServletOutputStream out = response.getOutputStream();
        ) {
            IOUtils.copy(in, out);
            return "导出成功!";
        } catch (Exception e) {
            log.error("导出文件异常:", e);
        }

        return "导出失败!";
    }

}

第二步:
新建要导出的列表类

package com.lc.yangzi.module.marketing.sell.customerinfo.domain;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;

@Data
public class CustomerInfoExcel {

    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"客户名称"}, index = 0)
    private String customerName;//客户名称
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"客户等级"}, index = 1)
    private Integer customerLevel;//客户等级:A,B,C,D
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"地址"}, index = 2)
    private String address;//地址
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"联系电话"}, index = 3)
    private String contactPhone;//联系电话
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"审核状态"}, index = 4)
    private String auditStatusName;//状态
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"提货状态"}, index = 5)
    private String pickingStatusName;//提货状态
    @ColumnWidth(20) // 定义列宽
    @ExcelProperty(value = {"管理人"}, index = 6)
    private String managerUserName;//管理人姓名(多个逗号分隔)


    @ExcelIgnore
    private Integer auditStatus;//审核状态审核状态:0草稿1审核中2已完结
    @ExcelIgnore
    private Long id;//主键ID
    @ExcelIgnore
    private Long orgId;//和组织管联 yz_sys_org where category = 3、
    @ExcelIgnore
    private Integer pickingStatus;//提货状态
    @ExcelIgnore
    private Long createUserId;//创建人
}

第四步:
调用方法

    //导出Excel
    @RequestMapping(value="/exportExcel", method = RequestMethod.GET)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) {
        try {
            String customerId = request.getParameter("customerId");
            String auditStatus = request.getParameter("auditStatus");
            String pickingStatus = request.getParameter("pickingStatus");

            if(StringUtils.isBlank(customerId)){
                customerId="0";
            }if(StringUtils.isBlank(auditStatus)){
                auditStatus="-1";
            }if(StringUtils.isBlank(pickingStatus)){
                pickingStatus="0";
            }
            List<CustomerInfoExcel> list = customerInfoService.selectAllCustomerInfoExcel(Long.parseLong(customerId),Integer.parseInt(auditStatus),Integer.parseInt(pickingStatus));
            ExcelUtils.export2Web(response, "客户信息", "客户信息", CustomerInfoExcel.class, list);
        } catch (Exception e) {
            log.error("报表导出异常:", e);
        }
    }
GitHub 加速计划 / ea / easyexcel
23
5
下载
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:23 天前 )
c42183df Bugfix 1 年前
efa7dff6 * 重新加回 `commons-io` 1 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐