excel动态模板导出

导入maven jar

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.17</version>
</dependency>

<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>${poi.version}</version>
</dependency>

<dependency>
   <groupId>net.sf.jxls</groupId>
   <artifactId>jxls-core</artifactId>
   <version>1.0.6</version>
</dependency>

springboot 导出类

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

/**
 * 导出Excel文档
 *
 * @author Tom
 */
public final class ExportToExcel {
    private static File root = null;

    public static final void export(String templet, OutputStream os,
                                    Map<Object, Object> map, InputStream is) throws Exception {
        if (templet == null)
            throw new IOException("该单未上传附件.");
        if (map == null)
            throw new IOException("该单未上传附件.");
        XLSTransformer transformer = new XLSTransformer();
        Workbook resultWorkbook = transformer.transformXLS(is, map);
        is.close();
        resultWorkbook.write(os);
        os.flush();
    }

    public static final void exportToResponse(String templet,
                                              HttpServletResponse response, Map<Object, Object> map,
                                              String exportFile, InputStream input) throws IOException {
        if (templet == null)
            throw new IOException("该单未上传附件.");
        if (response == null)
            throw new IOException("该单未上传附件.");
        if (map == null)
            throw new IOException("该单未上传附件.");
        if (StringUtils.isEmpty(exportFile)) {
            exportFile = templet;
        }
        exportFile = new String(exportFile.getBytes(), "UTF-8");
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment;filename=\""
                + exportFile + "\"");
        OutputStream os = response.getOutputStream();
        try {
            export(templet, os, map, input);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            response.flushBuffer();
            os.close();
        }

    }
}

 

String eFile ="test.xls";
String templet = "cd_packinglist_invoice.xls";
InputStream is = this.getClass().getResourceAsStream("/test/test.xls");
ExportToExcel.exportToResponse(templet, response, map, eFile,is);

 

<jx:if test="${mpa.test=='0'}">

</jx:if>
每个标签必须站一个表格。不可有空格。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值