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

相关推荐
敖正炀5 分钟前
CountDownLatch详解
java
敖正炀6 分钟前
Java 并发工具解析
java
SimonKing7 分钟前
JDK 24 新特性解析:更安全、更高效、更易用
java·后端·架构
敖正炀8 分钟前
CyclicBarrier详解
java
敖正炀8 分钟前
Java 8 中的 ConcurrentHashMap 详解
java
敖正炀9 分钟前
Exchanger详解
java
敖正炀10 分钟前
条件变量(Condition)详解
java
Java技术小馆11 分钟前
如何排查Linux系统中的CPU使用率过高问题
java·后端·面试
podongfeng28 分钟前
leetcode每日一题:数组美丽值求和
java·算法·leetcode·数组·前后缀
Ai 编码助手32 分钟前
PHP泛型与集合的未来:从动态类型到强类型的演进
java·python·php