java使用poi-tl模版引擎导出word之if判断条件的使用

文章目录

模版中if语句条件的使用

  • 如果区块对的值是 null 、false 或者空的集合,位于区块中的所有文档元素将不会显示,这就等同于if语句的条件为 false。
  • 语法示例:{{?status}}不亦君子乎{{/status}},status是你定义的boolean类型的变量。

首先制作word模版如下:

1.数据为False或空集合

(1)数据模型

json 复制代码
{
  "status": false
}

(2)完整接口代码

java 复制代码
    @GetMapping("/exportWord")
    public void exportWord(HttpServletResponse response) throws FileNotFoundException {
        //存放数据,也就是填充在word里面的值
        Map<String, Object> params = new HashMap<>();
        params.put("status",false);

        //模板路径
        // String templatePath = "E:\\demo\\word.docx";
        // 或模板在静态资源的相对路径
        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).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());
        }
    }

(3)运行结果:

2.非False或非空集合

(1)数据模型

json 复制代码
{
  "status": true
}

(2)完整接口代码

java 复制代码
    @GetMapping("/exportWord")
    public void exportWord(HttpServletResponse response) throws FileNotFoundException {
        //存放数据,也就是填充在word里面的值
        Map<String, Object> params = new HashMap<>();
        params.put("status",true);

        //模板路径
        // String templatePath = "E:\\demo\\word.docx";
        // 或模板在静态资源的相对路径
        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).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());
        }
    }

(3)运行结果

可以看到status为true时候,模版中内容都显示出来了!

相关推荐
衍生星球1 分钟前
Maven 3.9.6的下载和配置
java·maven·springboot
缘来是庄8 分钟前
设计模式之代理模式
java·设计模式·代理模式
都叫我大帅哥14 分钟前
向量数据库Milvus:非结构化数据的救星,AI开发者的瑞士军刀
java·python
comeilmforever30 分钟前
IDEA2025 Version Control 窗口 local changes显示
java·ide·intellij-idea
火车叨位去194931 分钟前
映射阿里云OSS(对象存储服务)
java·spring
2301_14725836937 分钟前
7月1日作业
java·前端·算法
Ting-yu41 分钟前
Java中Stream流的使用
java·开发语言·windows
一只猿Hou1 小时前
java分页插件| MyBatis-Plus分页 vs PageHelper分页:全面对比与最佳实践
java·mybatis
程序员弘羽1 小时前
C++ 第四阶段 内存管理 - 第二节:避免内存泄漏的技巧
java·jvm·c++
旷世奇才李先生1 小时前
Tomcat 安装使用教程
java·tomcat