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


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

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

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

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

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

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

  • 相关阅读:
    winform开发小技巧
    OpenCV实现多角度多尺度模板匹配(基于形状)
    To enable Secure Boots and Flash Encryption using the ESP Flash download tool
    初识Redis与桌面客户端
    基于单片机的超声波测距仪
    猫头虎博主第六期赠书活动:《手机摄影短视频和后期从小白到高手》
    论语第三篇-八侑
    电脑一键重装系统后如何打开事件查看器
    [Linux/初学者]用户管理
    浅略/逐行分析园区网接入交换机配置(以Ruijie交换机为例)
  • 原文地址:https://blog.csdn.net/zhx0114/article/details/126174512