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

相关推荐
Chase_Mos1 小时前
Spring 必会之微服务篇(1)
java·spring·微服务
小林学习编程3 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端
撸码到无法自拔3 小时前
docker常见命令
java·spring cloud·docker·容器·eureka
heart000_14 小时前
IDEA 插件推荐:提升编程效率
java·ide·intellij-idea
ŧ榕树先生4 小时前
查看jdk是否安装并且配置成功?(Android studio安装前的准备)
java·jdk
未来的JAVA高级开发工程师4 小时前
适配器模式
java
LUCIAZZZ4 小时前
JVM之内存管理(一)
java·jvm·spring·操作系统·springboot
D_aniel_5 小时前
排序算法-计数排序
java·排序算法·计数排序
一把年纪学编程5 小时前
【牛马技巧】word统计每一段的字数接近“字数统计”
前端·数据库·word
极小狐5 小时前
极狐GitLab 通用软件包存储库功能介绍
java·数据库·c#·gitlab·maven