easypoi使用模板导出所遇的坑

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

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

重写文件加载类 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>

你可能感兴趣的:(Java)