EasyExcel使用的正确姿势,工具类封装

2021-08-27 17:03:17 浏览数 (1)

项目中很可能用到导出excel文件的需求。

easyexcel代码量较小,使用简单,而且性能较佳,是一个非常好的选择。

源码地址:https://github.com/alibaba/easyexcel

工具类使用方法很简单:

1、maven依赖

代码语言:javascript复制
    com.alibaba
    easyexcel
    1.1.1

2、工具类

代码语言:javascript复制
package com.chujianyun.libs.excelpdf.easyexcel;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.commons.io.Charsets;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang3.ObjectUtils;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * EasyExcel工具类
 *
 * @author liuwangyanghdu@163.com
 * @date 2018/11/19
 */
public class EasyExcelUtil {


    /**
     * 下载EXCEL文件2007版本
     *
     * @throws IOException IO异常
     */
    public static void exportExcel2007Format(EasyExcelParams excelParams) throws IOException {
        exportExcel(excelParams, ExcelTypeEnum.XLSX);
    }

    /**
     * 下载EXCEL文件2003版本
     *
     * @throws IOException IO异常
     */
    public static void exportExcel2003Format(EasyExcelParams excelParams) throws IOException {
        exportExcel(excelParams, ExcelTypeEnum.XLS);
    }

    /**
     * 根据参数和版本枚举导出excel文件
     *
     * @param excelParams 参数实体
     * @param typeEnum    excel类型枚举
     * @throws IOException
     */
    private static void exportExcel(EasyExcelParams excelParams, ExcelTypeEnum typeEnum) throws IOException {
        Validate.isTrue(excelParams.isValid(), "easyExcel params is not valid");

        HttpServletResponse response = excelParams.getResponse();
        ServletOutputStream out = response.getOutputStream();
        ExcelWriter writer = new ExcelWriter(out, typeEnum, excelParams.isNeedHead());
        prepareResponds(response, excelParams.getExcelNameWithoutExt(), typeEnum);
        Sheet sheet1 = new Sheet(1, 0, excelParams.getDataModelClazz());
        if (StringUtils.isNotBlank(excelParams.getSheetName())) {
            sheet1.setSheetName(excelParams.getSheetName());
        }
        writer.write(excelParams.getData(), sheet1);
        writer.finish();
        out.flush();
    }


   /**
     * 将文件输出到浏览器(导出文件)
     * @param response 响应
     * @param fileName 文件名(不含拓展名)
     * @param typeEnum excel类型
     */
    private static void prepareResponds(HttpServletResponse response, String fileName, ExcelTypeEnum typeEnum) {
        String fileName2Export = new String((fileName).getBytes(Charsets.UTF_8), Charsets.ISO_8859_1);
        response.setContentType("multipart/form-data");
        response.setHeader("Content-disposition", "attachment;filename="   fileName2Export   typeEnum.getValue());
    }

    public static class EasyExcelParams {

        /**
         * excel文件名(不带拓展名)
         */
        private String excelNameWithoutExt;
        /**
         * sheet名称
         */
        private String sheetName;
        /**
         * 是否需要表头
         */
        private boolean needHead = true;
        /**
         * 数据
         */
        private List data;

        /**
         * 数据模型类型
         */
        private Class dataModelClazz;

        /**
         * 响应
         */
        private HttpServletResponse response;


        public EasyExcelParams() {
        }


        /**
         * 检查不允许为空的属性
         */
        public boolean isValid() {
            return ObjectUtils.allNotNull(excelNameWithoutExt, data, dataModelClazz, response);
        }

        public String getExcelNameWithoutExt() {
            return excelNameWithoutExt;
        }

        public void setExcelNameWithoutExt(String excelNameWithoutExt) {
            this.excelNameWithoutExt = excelNameWithoutExt;
        }

        public String getSheetName() {
            return sheetName;
        }

        public void setSheetName(String sheetName) {
            this.sheetName = sheetName;
        }

        public boolean isNeedHead() {
            return needHead;
        }

        public void setNeedHead(boolean needHead) {
            this.needHead = needHead;
        }

        public List getData() {
            return data;
        }

        public void setData(List data) {
            this.data = data;
        }

        public Class getDataModelClazz() {
            return dataModelClazz;
        }

        public void setDataModelClazz(Class dataModelClazz) {
            this.dataModelClazz = dataModelClazz;
        }

        public HttpServletResponse getResponse() {
            return response;
        }

        public void setResponse(HttpServletResponse response) {
            this.response = response;
        }
    }

}

其中数据模型的范例:

代码语言:javascript复制
package com.chujianyun.libs.excelpdf.easyexcel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;

/**
 * @author liuwangyanghdu@163.com 明明如月
 * @date 2018/11/19
 */
public class ExportInfo extends BaseRowModel {

    @ExcelProperty(value = "姓名" ,index = 0)
    private String name;

    @ExcelProperty(value = "年龄",index = 1)
    private String age;

    @ExcelProperty(value = "邮箱",index = 2)
    private String email;

    @ExcelProperty(value = "地址",index = 3)
    private String address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

使用时构造参数对象,调用导出excel的函数即可。

0 人点赞