java使用poi-tl模版引擎导出word之列表循环数据渲染

目录

poi-tl模版引擎中,如果区块对的值是一个非空集合,区块中的文档元素会被迭代渲染一次或者N次,这取决于集合的大小,类似于foreach语法。

1.模版制作

在静态资源目录下resources/static/templates新建exportWord.docx,编写以下模版内容:

2.开启spring表达式

(1)引入依赖

xml 复制代码
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>5.3.18</version>
</dependency>

(2)代码中开启表达式

java 复制代码
ConfigureBuilder builder = Configure.builder();
// 开启spring表达式
builder.useSpringEL();

3.编写关键代码接口

java 复制代码
@GetMapping("/exportWord")
public void exportWord(HttpServletResponse response) throws FileNotFoundException {
    //存放数据,也就是填充在word里面的值
    Map<String, Object> params = new HashMap<>();
    params.put("title","测试使用poi-tl模版导出word");

    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map = new HashMap<>();
    map.put("a","测试");
    map.put("b","米");
    map.put("c","201312");
    list.add(map);
    Map<String, Object> map1 = new HashMap<>();
    map1.put("a","测试2");
    map1.put("b","千米");
    map1.put("c","2012312");
    list.add(map1);

    params.put("list",list);

    ConfigureBuilder builder = Configure.builder();
    // 开启spring表达式
    builder.useSpringEL();

    // 或模板在静态资源的相对路径
    File rootFile = new File((ResourceUtils.getURL("classpath:").getPath()));
    File templateFile = new File(rootFile, "/static/templates/exportWord.docx");
    //jar包获取不到文件路径`
    //URLDecoder.decode() 解决获取中文名称文件路径乱码
    String templatePath = URLDecoder.decode(templateFile.getPath());
    //生成文件名
    String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + System.currentTimeMillis();
    // 导出wold
    try {
        // 导出Word文档为文件
        XWPFTemplate template = XWPFTemplate.compile(templatePath,builder.build()).render(params);
        // 将导出的Word文件转换为流
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition","attachment;filename=\""+fileName+".docx"+"\"");
        // HttpServletResponse response
        OutputStream out = response.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(out);
        template.write(bos);
        bos.flush();
        out.flush();
        // 最后不要忘记关闭这些流。
        PoitlIOUtils.closeQuietlyMulti(template, bos, out);
    } catch (Exception e) {
        System.out.println("导出Word文档时出现异常:" + e.getMessage());
    }
}

4. 导出结果

相关推荐
草莓熊Lotso1 小时前
C++ STL set 系列完全指南:从底层原理、核心接口到实战场景
开发语言·c++·人工智能·经验分享·网络协议·算法·dubbo
珹洺1 小时前
Java-Spring实战指南(三十四)Android Service实现后台音乐播放功能
android·java·spring
咖啡の猫3 小时前
搭建Python开发环境
开发语言·python
微学AI4 小时前
Rust语言的深度剖析:内存安全与高性能的技术实现操作
java·安全·rust
程序猿小蒜4 小时前
基于springboot的共享汽车管理系统开发与设计
java·开发语言·spring boot·后端·spring·汽车
lsp程序员0104 小时前
使用 Web Workers 提升前端性能:让 JavaScript 不再阻塞 UI
java·前端·javascript·ui
听风吟丶5 小时前
Java 8 Stream API 高级实战:从数据处理到性能优化的深度解析
开发语言·python
q***46525 小时前
在2023idea中如何创建SpringBoot
java·spring boot·后端
hygge9995 小时前
Spring Boot + MyBatis 整合与 MyBatis 原理全解析
java·开发语言·经验分享·spring boot·后端·mybatis
q***25215 小时前
Spring Boot接收参数的19种方式
java·spring boot·后端