poi-tl模板生成word,ZipEntry压缩文件导出 

依赖

<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.9.1</version>
</dependency>

代码

public void test(HttpServletResponse response) throws Exception {

        OutputStream out = setResponse(response, "测试.zip");
        ZipOutputStream zipOutputStream = new ZipOutputStream(out);

        HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy();
        Configure config = Configure.builder()
                .bind("zyList", hackLoopTableRenderPolicy)
                .bind("tables", hackLoopTableRenderPolicy)
                .build();
        List tables = Lists.newArrayList(wordUtil.toData(), wordUtil.toData());

        for(int i = 0; i < 2; i++){
            ClassPathResource resource = new ClassPathResource("file/测试.docx");
            //创建压缩文件
            ZipEntry zipEntry = new ZipEntry(i + ".docx");
            zipOutputStream.putNextEntry(zipEntry);
            ZipSecureFile.setMinInflateRatio(-1.0d);

            XWPFTemplate template = XWPFTemplate.compile(resource.getInputStream(),config) .render(Maps.newHashMap(ImmutableMap.of("tables", tables)));
            template.write(zipOutputStream);
        }
        zipOutputStream.close();
        out.close();
    }
    
    private OutputStream setResponse(HttpServletResponse response, String zipFileName) throws Exception{
        zipFileName = URLEncoder.encode(zipFileName, "UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=" + zipFileName);
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "no-store");
        response.addHeader("Cache-Control", "max-age=0");
        return response.getOutputStream();
    }

 数据

public class Data {
    private List<ZY> zyList;
    private String inName = "测试股份有限公司";
    private String inBCode = "A0095000001";
    private String inZCode = "B0002935";
    private String outName = "测试银行股份有限公司";
    private String outBCode = "A0011000001";
    private String outZCode = "A0000162";
    private String bCode = "190013";
    private String bName = "19测试国债13";
    private String bAmount = "17000";
    private String bFCode = "A0095000001";
    private String bSCode = "A0011000001";
    private String inOrgnaization = "A";
    private String outOrgnaization = "B";
    private String date = "2022年01月12日";
}

public class ZY {
    private String code;
    private String name;
    private String amount;
}

public static Data toData(){
    Data da = new Data();
    ZY zy1 = new ZY("012101520", "21测试国投SCP002", "4000");
    ZY zy2 = new ZY("101801532", "18测试国资MTN003", "1000");
    ZY zy3 = new ZY("101900578", "19测试城投MTN002", "10000");
    ZY zy4 = new ZY("102100792", "21测试投资MTN001", "2000");
    List list = Lists.newArrayList(zy1,zy2, zy3, zy4);
    da.setZyList(list);
    return da;
}

  模板

resources/file/测试.docx

 结果

 

posted @ 2022-07-15 14:21  初六丶  阅读(724)  评论(0编辑  收藏  举报