下载 word(freemark遍历. ftl)

本文介绍了一个使用FreeMarker模板引擎将数据填充到Word文档的方法,并提供了完整的代码示例,展示了如何从数据库加载档案信息、设置模板、生成文档并提供下载。
@RequestMapping(value = "exportword") public void exportword(BizUserArchive bizUserArchive,HttpServletRequest request, HttpServletResponse response) { bizUserArchive = bizUserArchiveService.loadArchiveByUserId(bizUserArchive); Map dataMap = new HashMap();dataMap.put("personName",bizUserArchive.getPersonName());dataMap.put("flist",bizUserArchive.getBizUserFamily()); File outFile = null; try {configuration.setDefaultEncoding("UTF-8");configuration.setClassForTemplateLoading(this.getClass(), ""); // FTL文件所存在的位置Template template = configuration.getTemplate("formWord.ftl");outFile = new File("D:/downFile/formWord.doc");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));template.process(dataMap, out);out.close(); // path是指欲下载的文件的路径。// File file = new File("G:/formWord.doc"); // 取得文件名。 String filename = outFile.getName(); // 取得文件的后缀名。 String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase(); // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(outFile)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes())); response.addHeader("Content-Length", "" + outFile.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); // response.setContentType("application/octet-stream"); response.setContentType("application/msword");//application/msword toClient.write(buffer); toClient.flush(); toClient.close(); } catch (Exception e) {e.printStackTrace();}finally {outFile.delete();//删除每次下载的文件}}
在Java中使用FreeMarker通过FTL文件生成带目录的Word文档,可按以下步骤进行操作: ### 1. 配置FreeMarker环境 首先要确保项目里添加了FreeMarker的依赖。若使用Maven,可在`pom.xml`中添加如下依赖: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> ``` ### 2. 创建FTL模板文件 创建一个FTL模板文件(例如`template.ftl`),在模板里设置好文档的结构与内容,并且要为需要生成目录的标题设置好样式与层级。示例如下: ```xml <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> <w:body> <!-- 目录 --> <w:p> <w:r> <w:t>目录</w:t> </w:r> </w:p> <w:p> <w:r> <w:fldChar w:fldCharType="begin"/> </w:r> <w:r> <w:instrText xml:space="preserve">TOC \o "1-3" \h \z \u</w:instrText> </w:r> <w:r> <w:fldChar w:fldCharType="end"/> </w:r> </w:p> <!-- 正文内容 --> <w:p> <w:pPr> <w:pStyle w:val="Heading1"/> </w:pPr> <w:r> <w:t>第一章 引言</w:t> </w:r> </w:p> <w:p> <w:r> <w:t>这是引言部分的内容。</w:t> </w:r> </w:p> <w:p> <w:pPr> <w:pStyle w:val="Heading2"/> </w:pPr> <w:r> <w:t>1.1 背景</w:t> </w:r> </w:p> <w:p> <w:r> <w:t>这是背景部分的内容。</w:t> </w:r> </w:p> </w:body> </w:document> ``` 在上述模板中,`TOC \o "1-3" \h \z \u` 是用于生成目录的Word域代码,它会依据标题样式(如`Heading1`、`Heading2`等)来生成目录。 ### 3. Java代码实现 编写Java代码来加载FTL模板文件,填充数据,然后生成Word文档。示例代码如下: ```java import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import java.io.*; import java.util.HashMap; import java.util.Map; public class FreeMarkerWordGenerator { public static void main(String[] args) { try { // 配置FreeMarker Configuration cfg = new Configuration(Configuration.VERSION_2_3_31); cfg.setDirectoryForTemplateLoading(new File("path/to/templates")); // 设置模板文件所在目录 // 加载模板 Template template = cfg.getTemplate("template.ftl"); // 准备数据模型 Map<String, Object> data = new HashMap<>(); // 可根据需要添加更多数据 // 生成Word文档 File outputFile = new File("output.docx"); try (Writer out = new OutputStreamWriter(new FileOutputStream(outputFile), "UTF-8")) { template.process(data, out); } System.out.println("Word文档生成成功!"); } catch (IOException | TemplateException e) { e.printStackTrace(); } } } ``` ### 4. 注意事项 - 要保证FTL模板文件里的标题样式与Word中的标题样式一致,这样才能正确生成目录。 - 生成的Word文档可能需要在打开后手动更新目录,以确保目录内容是最新的。可以通过在Word中右键点击目录,选择“更新域”来更新目录。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值