使用xml、ftl模板生成word文档,下载到浏览器或指定位置

  1. 把模板word需要填充的地方写入内容并保存,然后另存为Word 2003 XML 文档(*.xml),把模板放到系统内。
  2. 根据填充的内容,找到对应的位置,使用<![CDATA[${tml}]]>替代掉填充内容,${tml}就是服务端传回此处内容的key,按照自己的习惯定义即可,<![CDATA[ ]]>的作用主要是防止服务端传参为空,或者没传会报错的问题,如果数据为List则使用list标签遍历,如下:
    <w:tc>
        <w:tcPr>
        <w:tcW w:w="1256" w:type="dxa"/>
        </w:tcPr>
        <#list testMethodList as tml>
    	    <w:p wsp:rsidR="002D3582" wsp:rsidRDefault="002D3582" wsp:rsidP="00FE67C7">
    	    <w:pPr>
    	        <w:jc w:val="center"/>
    	        <w:rPr>
    	        <w:sz w:val="22"/>
    	        <w:sz-cs w:val="22"/>
    	        </w:rPr>
    	        </w:pPr>
    	        <aml:annotation aml:id="1" w:type="Word.Bookmark.Start" w:name="Col_TestMeth"/>
    	        <aml:annotation aml:id="1" w:type="Word.Bookmark.End"/>
    	        <w:r>
    	            <w:rPr>
    	            <w:sz w:val="22"/>
    	            <w:sz-cs w:val="22"/>
    	            </w:rPr>
    	            <w:t><![CDATA[${tml}]]></w:t>
    	        </w:r>
    	   </w:p>
        </#list>
    </w:tc>

    特别注意试项:word文档是按列进行数据填充的,所以需要按照列的方式组织数据,但是又要兼顾每一行数据的位置对应

  3. 文件下载的方式:第一种是使用浏览器下载,代码如下,(参数说明:fileName为模板名称,带后缀;outName为文件输出时的名称,带后缀;writeData为模板中需要填充数据的map)(需要引入freemarker的jar):

  4. response.setContentType("application/msword");
    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(outName, "UTF-8"));
    Writer writer = response.getWriter();
    Configuration cfg=new Configuration();
    String path =FtlHandler.class.getResource("/").getPath()+"ftl";
    TemplateLoader templateLoader = new FileTemplateLoader(new File(path));
    cfg.setTemplateLoader(templateLoader);
    cfg.setDefaultEncoding("UTF-8");//设置模板读取的编码方式,用于处理乱码
    Template template = cfg.getTemplate(fileName,"UTF-8");//模板文件,支持xml,ftl 也支持html
    template.process(writeData, writer);//将模板写到文件中
    writer.flush();
    cfg.clearTemplateCache();
    writer.close();

    第二种是下载到指定的位置,(参数说明:newTemp.ftl为模板名称;datas为模板中需要填充数据的map):

    Configuration cfg = new Configuration(); // 通过Freemaker的Configuration读取相应的ftl
    TemplateLoader templateLoader = new FileTemplateLoader(new File(AutoDownloadReportService.class.getResource("/").getPath() + "ftl"));
    cfg.setTemplateLoader(templateLoader);
    cfg.setDefaultEncoding("UTF-8");// 设置模板读取的编码方式,用于处理乱码
    Template template = cfg.getTemplate("newTemp.ftl", "UTF-8");// 模板文件,支持xml,ftl 也支持html
    File file = new File(path + "\\" + "胶料报告" + gnumber + " " + batch + ".doc");
    if (!file.getParentFile().exists())
    { // 判断有没有父路径,就是判断文件整个路径是否存在
        file.getParentFile().mkdir(); // 不存在就全部创建
    }
    Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
    template.process(datas, out);
    out.flush();
    out.close();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
FreeMarker 是一个Java模板引擎,它可以帮助我们根据模板生成各种文件,包括 Word 文档。下面是一个简单的示例,演示如何使用 FreeMarker 创建一个根据 FTL 模板生成 Word 文档的过程: 1. 创建一个 FreeMarker 的配置对象,并设置模板文件所在的目录: ``` Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setDirectoryForTemplateLoading(new File("path/to/ftl/templates")); ``` 2. 从配置对象中获取模板对象: ``` Template template = cfg.getTemplate("template.ftl"); ``` 3. 准备数据模型,可以是一个 JavaBean、Map 或者其他类型的对象: ``` Map<String, Object> data = new HashMap<>(); data.put("title", "Hello, World!"); data.put("content", "This is a test document created by FreeMarker."); ``` 4. 创建一个 Writer 对象,用于输出生成的 Word 文档: ``` Writer out = new FileWriter(new File("path/to/output/doc.docx")); ``` 5. 将数据模型和 Writer 对象传递给模板对象,生成 Word 文档: ``` template.process(data, out); ``` 完整的示例代码如下: ``` import freemarker.template.Configuration; import freemarker.template.Template; import java.io.File; import java.io.FileWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; public class FreeMarkerDemo { public static void main(String[] args) throws Exception { // 创建 Configuration 对象 Configuration cfg = new Configuration(Configuration.VERSION_2_3_30); cfg.setDirectoryForTemplateLoading(new File("path/to/ftl/templates")); // 获取模板对象 Template template = cfg.getTemplate("template.ftl"); // 准备数据模型 Map<String, Object> data = new HashMap<>(); data.put("title", "Hello, World!"); data.put("content", "This is a test document created by FreeMarker."); // 创建输出流 Writer out = new FileWriter(new File("path/to/output/doc.docx")); // 生成 Word 文档 template.process(data, out); // 关闭输出流 out.close(); } } ``` 注意,上面的示例代码中使用模板文件是 FTL 格式,如果要生成 Word 文档,还需要将模板文件转换成 docx 或者其他 Word 文档格式。常见的工具包括 Apache POI 和 Docx4j 等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值