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 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen2 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研2 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
没有bug.的程序员2 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
甄超锋3 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
阿华的代码王国3 小时前
【Android】RecyclerView复用CheckBox的异常状态
android·xml·java·前端·后端
Zyy~3 小时前
《设计模式》装饰模式
java·设计模式
A尘埃3 小时前
企业级Java项目和大模型结合场景(智能客服系统:电商、金融、政务、企业)
java·金融·政务·智能客服系统
青云交4 小时前
Java 大视界 -- 基于 Java 的大数据可视化在城市交通拥堵治理与出行效率提升中的应用(398)
java·大数据·flink·大数据可视化·拥堵预测·城市交通治理·实时热力图
CHEN5_024 小时前
【Java基础面试题】Java基础概念
java·开发语言