easypoi使用模板导出所遇的坑

做这个功能的时候躺了很多坑,当时错误信息没记下来,错误信息简单总结下,希望能帮助到有需要的人!
1、找不到模板路径
排查步骤:

<!-- 资源打包加载 -->
<!-- include中一定要加载加入新增的文件夹 -->
<resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>META-INF/**</include>
                    <include>mapper/**</include>
                    <include>config/application-${activatedProperties}.yml</include>
                    <include>*.xml</include>
                    <include>*.yml</include>
                    <include>*.properties</include>
                    <include>template/**</include>
                </includes>
            </resource>
        </resources>

重写文件加载类 FileLoaderImpl

package cn.gov.cwl.vlt.misc.easypoi;import cn.afterturn.easypoi.cache.manager.IFileLoader;
import cn.afterturn.easypoi.util.PoiPublicUtil;
import cn.gov.cwl.vlt.util.FileUtils;
import org.apache.poi.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;/**
 * 文件加载类,根据路径加载指定文件
 *
 * @author user
 * @date 2018/12/18
 */
public class FileLoaderImpl implements IFileLoader {private static final Logger LOGGER = LoggerFactory.getLogger(cn.afterturn.easypoi.cache.manager.FileLoaderImpl.class);@Override
    public byte[] getFile(String url) {
        InputStream fileis = null;
        ByteArrayOutputStream baos = null;
        try {//判断是否是网络地址
            if (url.startsWith("http")) {
                URL urlObj = new URL(url);
                URLConnection urlConnection = urlObj.openConnection();
                urlConnection.setConnectTimeout(30);
                urlConnection.setReadTimeout(60);
                urlConnection.setDoInput(true);
                fileis = urlConnection.getInputStream();
            } else {
                //先用绝对路径查询,再查询相对路径
                try {
                    fileis = new FileInputStream(url);
                } catch (FileNotFoundException e) {
                    //获取项目文件
                    fileis = FileLoaderImpl.class.getClassLoader().getResourceAsStream(url);
                    // ---------------重新新增代码开始点---------------
                    if (fileis == null) {
                        //最后再拿相对文件路径
                        String path = FileUtils.getWebRootPath(url);
                        fileis = new FileInputStream(path);
                    }
                    // ---------------重新新增代码结束点---------------
                }
            }
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileis.read(buffer)) > -1) {
                baos.write(buffer, 0, len);
            }
            baos.flush();
            return baos.toByteArray();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(fileis);
            IOUtils.closeQuietly(baos);
        }
        LOGGER.error(fileis + "这个路径文件没有找到,请查询");
        return null;
    }
}

在生产 workbook 之前,使用该代码 POICacheManager.setFileLoader(new FileLoaderImpl());加载重写的类

TemplateExportParams params = new TemplateExportParams("template/betcard.xlsx",0);
// 重要代码
POICacheManager.setFileLoader(new FileLoaderImpl());
Map<String, Object> map = new HashMap<String, Obj
map.put("maplist", cardInfoList);
map.put("batch",cardInfoList.get(0).getBatch());
map.put("cardType", CardTypeEnum.getCardNameByCode(cardInfoList.get(0).getCardType()));
map.put("cardSum",cardInfoList.size());
map.put("cardDate", LocalDate.
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
ExcelUtiles.downLoadExcel(fileName,response,workbook);

2、模板文件压缩不可用问题

<!-- 资源文件拷贝插件 -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <configuration>
        <encoding>UTF-8</encoding>
        <!-- 过滤后缀文件 -->
        <nonFilteredFileExtensions>
            <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
            <nonFilteredFileExtension>xls</nonFilteredFileExtension>
        </nonFilteredFileExtensions>
    </configuration>
</plugin>
easypoi通过模板导出excel的步骤如下: 1. 准备数据:首先,需要准备好要填充到模板中的数据。可以用一个list中的map来表示每一页的数据。每个map对应着一页文档的数据,而list中的每个元素对应着一页文档。例如,可以创建一个List<Map<String, Object>>来存储数据。 2. 设置模板和参数:然后,需要设置模板路径和参数。可以使用TemplateExportParams类来读取模板,设置sheet名,并且将数据map传入。 3. 导出excel:接下来,使用ExcelExportUtil.exportExcel方法导出excel。该方法接受模板参数和数据参数,并返回一个Workbook对象。 4. 写出excel:最后,将Workbook对象写出到输出流中即可下载。可以通过调用Workbook的write方法将数据写入到response的输出流中。 需要注意的是,导出过程中可能会出现样式问题,比如模板中的单元格合并了,但导出的excel并没有合并。这时可以通过手动处理来解决合并单元格的问题。 以上是通过easypoi通过模板导出excel的基本步骤。你可以参考提供的代码示例和使用easypoi的相关文档来具体实现。 [2 [3<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Easypoi使用模板导出文档或excel表格详解](https://blog.csdn.net/qq_44845339/article/details/110537034)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* [使用easyPoi根据模板导出excel](https://blog.csdn.net/weixin_44854514/article/details/120311371)[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^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值