poi-tl官网:Poi-tl Documentation 

1.导入包

        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi-ooxml</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


2.制作word模板,模板中参数用{{param}}形式,更多标签形式参考官网文档

3.导出word代码

    @PostMapping("/download")
    public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ClassPathResource cp = new ClassPathResource("resource路径下的文件名.docx");
        File file = cp.getFile();
        Map<String, Object> params = new HashMap<>();
        params.put("name", "张三");
        params.put("place", "李四家里");
        params.put("something", "一顿饭");
        XWPFTemplate xwpfTemplate = XWPFTemplatepile(file).render(params);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xwpfTemplate.write(byteArrayOutputStream);
        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();
        xwpfTemplate.close();
        byte[] bytes = byteArrayOutputStream.toByteArray();
        // 输出流写入文件字节数组
        String time = LocalDateTime.now().
            format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
        String exportFileName = "文件名_"+ time +".docx";
        String header = request.getHeader("user-agent").toLowerCase();
        if (header.contains("mozilla")) {//google,火狐浏览器
            exportFileName = new String(exportFileName.getBytes(), "ISO8859-1");
        } else {
            exportFileName = URLEncoder.encode(exportFileName, "UTF-8");//其他浏览器
        }
        response.setContentType("application/octet-stream;charset=ISO8859-1");
        response.setHeader("Content-Disposition","attachment;filename=\"" 
            + exportFileName + "\"");
        OutputStream outputStream=response.getOutputStream();
        outputStream.write(bytes);
        outputStream.flush();
        outputStream.close();
    }

===放到服务器找不到模板路径文件解决办法:

ClassPathResource cp = new ClassPathResource("resource路径下的文件名.docx");

File file = cp.getFile() 

上面两行代码修改为 

File file = this.fromClasspath("resource路径下的文件名.docx")

fromClasspath方法代码如下:

    public static File fromClasspath(String path) {
        // 在项目运行时能找到,单例类测试找不到,配置和jar包分离仍然可用
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            File file = null;
            // 解决jar保重无法获得file对象
            logger.info("当前文件协议:{}", classPathResource.getURL().getProtocol());
            if("jar".equalsIgnoreCase(classPathResource.getURL().getProtocol())) {
                if(System.getProperty("os.name").toLowerCase().contains("windows")) {
                    logger.info("windows环境!");
                    file = new File(System.getenv("temp"), classPathResource.getFilename());
                } else {
                    logger.info("linux环境!");
                    file = new File("/tmp", classPathResource.getFilename());
                }
                if(!file.exists()) {
                    file.createNewFile();
                    logger.info("创建文件!");
                    IOUtils.copy(classPathResource.getInputStream(), new FileOutputStream(file));
                }
            } else {
                logger.info("classPathResource.getFile()");
                file = classPathResource.getFile();
            }
            logger.info("当前文件路径:{}", file.getAbsolutePath());
//            InputStream inputStream = new ClassPathResource(path).getInputStream();
            return file;
        } catch (IOException e) {
            logger.error("classpath找不到资源: {}, 原因: {}", path, e.getMessage());
            return null;
        }
    }

更多推荐

poi-tl使用模板导出word文档