springboot使用poi-tl动态填充word模板

第一、导入依赖



    org.apache.poi
    poi
    4.1.2


    org.apache.poi
    poi-ooxml
    4.1.2


    org.apache.poi
    poi-ooxml-schemas
    4.1.2




    com.deepoove
    poi-tl
    1.10.0

第二、配置maven打包,过滤掉word文件,不加以编码,否则会报错


    org.apache.maven.plugins
    maven-resources-plugin
    3.1.0
    
        UTF-8
        
            
            xlsx
            xls
            docx
            doc
            pptx
            ppt
        
    

第三、编写测试代码

@GetMapping("/filld")
@ApiOperation("测试word代码填充")
public void zipDdownload2(HttpServletRequest request, HttpServletResponse response) throws IOException {
    Map params = new HashMap<>();
    params.put("legalCode","dddd");
    params.put("channel","数智xxx");
    Resource resource = new ClassPathResource("templates_report/2.docx");
    File file = resource.getFile();
    //创建输出流
    OutputStream os = new FileOutputStream("template1_out.docx");
    //最终编译渲染并输出
    XWPFTemplate.compile(file).render(params).writeAndClose(os);
    System.out.println("输出完毕");
}

你可能感兴趣的:(spring,boot,java,后端)