使用easy excel导出复杂表头的excel

使用easy-excel导出复杂表头的excel

今天想写一个双层表头的excel导出,一开始使用的是poi来画发现太麻烦,
于是就想到了使用easy-excel的模板填充来实现,将导出写成了一个简单的工具类,
供参考

在这里插入图片描述
最终是要实现为这样的一个效果 , 红色为表头,绿色为表体,于是我先做出了一个模板 ,
如果不会写模板的可以去看官方文档, 十分简单 https://www.yuque.com/easyexcel/doc/fill
在这里插入图片描述
然后将表头数据封装成一个map,表体数据为一个list , 去调用工具类就实现了
在这里插入图片描述
以下为工具类代码,导入了easy-excel就可以使用

package com.org.inventory.util;

import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Description: easy excel 工具类
 *
 * @ProjectName: inventory_task
 * @ProduceName: IntelliJ IDEA
 * @author: li ji hong
 * @date: 2021/12/15 9:51
 */
public class EasyExcelUtils {
    private static final String CONTENT_TYPE = "application/vnd.ms-excel";
    private static final String CHARACTER_ENCODING = "utf-8";
    private static final String HEADER_S1 = "Content-disposition";
    private static final String HEADER_S2 = "attachment; filename=";


    /**
     * 创建excel
     *
     * 此方法为封装简单excel的导出 ,
     * 表头或者日期等信息写在map ,
     * list数据单独传可以实现简单导出
     *
     * @param response 响应
     * @param fileName 文件名称
     * @param map      map
     * @param list     列表
     * @throws IOException ioexception
     */
    public static void createExcel(HttpServletResponse response, String fileName, Map<String, Object> map, List<?> list) throws IOException {
        // 设置公共头信息
        OutputStream out = null;
        BufferedOutputStream bos = null;
        response.setContentType(CONTENT_TYPE);
        response.setCharacterEncoding(CHARACTER_ENCODING);
        response.setHeader(HEADER_S1, HEADER_S2 + fileName);

        out = response.getOutputStream();
        bos = new BufferedOutputStream(out);
        // 此处可以修改 为 classpath:/file
        ClassPathResource cpr = new ClassPathResource("file" + File.separator + fileName);
        ExcelWriter excelWriter = com.alibaba.excel.EasyExcel.write(bos)
                .withTemplate(cpr.getInputStream()).excelType(ExcelTypeEnum.XLS).build();
        WriteSheet writeSheet = com.alibaba.excel.EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        // 填充 list 数据
        excelWriter.fill(list, fillConfig, writeSheet);
        // 填充 map 数据
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
        bos.flush();
        bos.close();


    }
}

直接调用即可

补充一下依赖,因为有些人的项目可能会与poi产生冲突

<!--集成EasyExcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.servlet</groupId>
                    <artifactId>servlet-api</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml-schemas</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
  • 7
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值