门户网站做吗,企业邮箱怎么获取,做公众号试卷的网站,亦庄网站建设公司文章目录 模版中if语句条件的使用1.数据为False或空集合2.非False或非空集合 模版中if语句条件的使用
如果区块对的值是 null 、false 或者空的集合#xff0c;位于区块中的所有文档元素将不会显示#xff0c;这就等同于if语句的条件为 false。语法示例#xff1a;{{?stat… 文章目录 模版中if语句条件的使用1.数据为False或空集合2.非False或非空集合 模版中if语句条件的使用
如果区块对的值是 null 、false 或者空的集合位于区块中的所有文档元素将不会显示这就等同于if语句的条件为 false。语法示例{{?status}}不亦君子乎{{/status}}status是你定义的boolean类型的变量。
首先制作word模版如下
1.数据为False或空集合
1数据模型
{status: false
}2完整接口代码 GetMapping(/exportWord)public void exportWord(HttpServletResponse response) throws FileNotFoundException {//存放数据也就是填充在word里面的值MapString, 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();// 导出woldtry {// 导出Word文档为文件XWPFTemplate template XWPFTemplate.compile(templatePath).render(params);// 将导出的Word文件转换为流response.setContentType(application/octet-stream);response.setHeader(Content-disposition,attachment;filename\fileName.docx\);// HttpServletResponse responseOutputStream 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数据模型
{status: true
}2完整接口代码 GetMapping(/exportWord)public void exportWord(HttpServletResponse response) throws FileNotFoundException {//存放数据也就是填充在word里面的值MapString, 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();// 导出woldtry {// 导出Word文档为文件XWPFTemplate template XWPFTemplate.compile(templatePath).render(params);// 将导出的Word文件转换为流response.setContentType(application/octet-stream);response.setHeader(Content-disposition,attachment;filename\fileName.docx\);// HttpServletResponse responseOutputStream 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时候模版中内容都显示出来了