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时候,模版中内容都显示出来了!

相关推荐
南宫生2 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长3 小时前
Maven 基础环境搭建与配置(一)
java·maven
风与沙的较量丶4 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_748251724 小时前
SpringBoot3 升级介绍
java
极客先躯5 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行5 小时前
Spring事务原理 二
java·后端·spring
小猫猫猫◍˃ᵕ˂◍5 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
liuyuzhongcc5 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
五月茶6 小时前
Spring MVC
java·spring·mvc
sjsjsbbsbsn6 小时前
Spring Boot定时任务原理
java·spring boot·后端