记 EasyExcel 使用的一次大坑,使用EasyExcel的错误。

一、前言

     EasyExcel 作为阿里的开源软件,(无数LOG)。最近在写文件下载的功能,需要将数据库中的数据进行Excel导出。官方写的文档很全了,我也不能直接搬人家的是吧。除了给出基本的方法,着重是 问题的解决,找了半天。

      你对着官网直接抄,如果不报错,那么你可以 ctrl + w。 如果报错则继续看吧。

    

二、入门

  2.1 添加maven依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.0.5</version>
</dependency>

     版本号自己看着办。目前最新的是2.2.6。

2.2 service 输出文件

@Async   //下载文件,肯定多线程
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) //异常
public void downLoadTransFile(PayOrderFlowQueryAllReq req, HttpServletResponse response) throws Exception {
    response.setContentType("application/vnd.ms-excel");  // excel 的响应头
    response.setCharacterEncoding("utf-8");
    // 文件名字
    String fileName = URLEncoder.encode(System.currentTimeMillis() + "yourName", "UTF-8");   
    response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    List<你的实体类> resp = 你的List集合的数据;
    EasyExcel.write(response.getOutputStream(), excel首行的column.class).sheet("模板").doWrite(resp);
}

2.3   excel首行的column.class

public class ddddd{

    @ExcelProperty(value = "你想写什么就写什么")
    private String a;

    @ExcelProperty(value = "excel 首行 ")
    private String b;

}

2.4 完了

三、报错 - 截取部分错误;省的密密麻麻的一堆没用的东西

错误:

错误1:Error   DOMSource cannot be processed: check that saxon8-dom.jar is on the classpath

错误2: com.alibaba.excel.exception.ExcelGenerateException: Can not close IO

错误3:Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException: Fail to save: an error occurs while saving the package : The part /docProps/core.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@3f93e206

错误4:Caused by: org.apache.poi.openxml4j.exceptions.OpenXML4JException: The part /docProps/core.xml failed to be saved in the stream with marshaller org.apache.poi.openxml4j.opc.internal.marshallers.ZipPackagePropertiesMarshaller@3f93e206

 。  。   。  。

解决:

    网上搜了一堆:增加 easyExcel 的版本,增加 poi的版本 等等。可能有用,但是我折腾了半天,最后发现,需要添加3个依赖,如果你添加两个问题就可以解决的话,第三个就可以不用添加了。

    新增的依赖:

  

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.0</version>
</dependency>
<dependency>
    <groupId>net.sf.saxon</groupId>
    <artifactId>saxon-dom</artifactId>
    <version>8.7</version>
    <scope>compile</scope>
</dependency>

  

EasyExcel自定义Converter是指在使用EasyExcel进行Excel读写操作时,可以自定义转换器来处理一些不支持的数据类型。通过自定义转换器,我们可以将不支持的数据类型转换为EasyExcel支持的数据类型,以便正确地进行读写操作。 在SpringBoot中使用EasyExcel时,可以通过编写自定义转换器来处理部分数据类型在导出时EasyExcel不支持的情况。首先,需要创建一个实现了Converter接口的转换器类,比如CharConverter。在CharConverter中,我们需要实现supportJavaTypeKey方法,返回需要支持的数据类型,这里是Character类型。然后,我们需要实现convertToExcelData方法,对Character类型的数据进行自定义处理,比如将其转换为String类型。最后,将CharConverter注入为一个Spring组件。 在写操作时,需要将自定义的转换器注册到EasyExcel中。可以通过调用registerConverter方法,并将自定义转换器作为参数传入来实现。例如,在写学生信息到Excel的方法中,可以通过registerConverter(new CharConverter())将CharConverter注册到EasyExcel中。 需要注意的是,在不同的模块中,注册自定义转换器的方式可能会有所不同。在某些模块中,可能需要使用build方法来创建WriteSheet对象,并在创建过程中注册转换器。例如,在创建WriteSheet对象时,可以通过registerConverter方法将自定义转换器注册到EasyExcel中。 通过自定义转换器,我们可以灵活地处理EasyExcel不支持的数据类型,确保数据的正确读写。 #### 引用[.reference_title] - *1* *2* [EasyExcel 自定义类型转换器](https://blog.csdn.net/weixin_45535519/article/details/130291453)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [15.EasyExcel自定义Convert遇到大坑!!!](https://blog.csdn.net/qq_37992410/article/details/126170990)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值