多个复杂excel模板打包zip下载

技术使用 jxls 模板生成  

1.pom:若和系统poi冲突 排除掉依赖即可

<dependency>
    <groupId>net.sf.jxls</groupId>
    <artifactId>jxls-core</artifactId>
    <version>1.0.6</version>
    <exclusions>
        <exclusion>
            <artifactId>poi</artifactId>
            <groupId>org.apache.poi</groupId>
        </exclusion>
        <exclusion>
            <artifactId>poi-ooxml</artifactId>
            <groupId>org.apache.poi</groupId>
        </exclusion>
        <exclusion>
            <artifactId>poi-ooxml-schemas</artifactId>
            <groupId>org.apache.poi</groupId>
        </exclusion>
    </exclusions>
</dependency>

2.excel 模板 例子 支持el 表达式

3.代码

/**
 * jxls 生成Workbook 
 *
 * @param beans
 * @param srcPath
 */
private Workbook exportTempleExcel(Map beans, String srcPath) {
    XLSTransformer transformer = new XLSTransformer();
    try {
        //获得模板的输入流
        FileInputStream in = new FileInputStream(srcPath);
        //将beans通过模板输入流写到workbook中
        return transformer.transformXLS(in, beans);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
    }
}
String zipFileName = "zipname";
//设置输出格式
response.setContentType("application/octet-stream");
response.addHeader("Content-disposition", "attachment; zipFileName =" + new String((zipFileName +  ".zip").getBytes("utf-8"), "ISO8859-1"));
response.addHeader("zipFileName ", StringUtils.urlEncode(zipFileName + ".zip"));

OutputStream  outputStream = null;
ZipOutputStream zipOutputStream = null;
try {
    outputStream = response.getOutputStream();
    zipOutputStream = new ZipOutputStream(outputStream);
//查询list      
Map<String, Map<String, Object>> downLoadMap = //查询数据 按格式输出
    if( downLoadMap.size()<1){
        return ResultConstants.RESULT_LIST_NULL_DESC;
    }
    String path = request.getServletContext().getRealPath("");
    String srcPath = path + File.separator + "/WEB-INF/classes/excels/转运单模板.xlsx";

    for (Map.Entry<String, Map<String, Object>> bean : downLoadMap.entrySet()) {
        String fileName = bean.getKey()+"转运单模板.xlsx";
        Workbook workbook =  exportTempleExcel(bean.getValue(), srcPath);
        //写入压缩包
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        workbook.write(os);
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        int readLen;
        byte[] buf = new byte[1024 * 5];
        zipOutputStream.putNextEntry(new ZipEntry(fileName));
        while ((readLen = is.read(buf, 0, 1024 * 5)) != -1) {
            zipOutputStream.write(buf, 0, readLen);
        }
        zipOutputStream.closeEntry();
        is.close();
    }
    zipOutputStream.flush();
} catch (Exception e) {
    return ResultConstants.DOWNLOAD_ERROR_DESC;
} finally {
    //提前下载完成 提前解锁
    try {
        //关闭数据流,注意关闭的顺序
        if(zipOutputStream != null){
            zipOutputStream.close();
        }
        if(outputStream != null){
            outputStream.close();
        }
      

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值