1、新建一个docx格式的word文档

2、编写模板内容,调整模板格式,将需要替换的内容用freemark语言标记,例如${content}

3、复制一份docx模板文件,修改扩展名为zip,解压文件,把word目录下document.xml文件复制出来

4、修改document.xml文件名和docx模板文件名一致,放入模板目录(2个文件都需要)

5、用编辑器把xml代码格式化,检查需要替换的数据参数是否显示正常

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:wpsCustomData="http://www.wps.cn/officeDocument/2013/wpsCustomData" mc:Ignorable="w14 w15 wp14"><w:body><w:p><w:pPr><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr><w:t>word模板</w:t></w:r></w:p><w:p><w:pPr><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr></w:pPr></w:p><w:p><w:pPr><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr><w:t>内容</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="default" /><w:lang w:eastAsia="zh-Hans" /></w:rPr><w:t>:</w:t></w:r></w:p><w:p><w:pPr><w:rPr><w:rFonts w:hint="default" /><w:lang w:eastAsia="zh-Hans" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="default" /><w:lang w:eastAsia="zh-Hans" /></w:rPr><w:t>${content}</w:t></w:r><w:bookmarkStart w:id="0" w:name="_GoBack" /><w:bookmarkEnd w:id="0" /></w:p><w:p><w:pPr><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="eastAsia" /><w:lang w:val="en-US" w:eastAsia="zh-Hans" /></w:rPr><w:t>结束</w:t></w:r><w:r><w:rPr><w:rFonts w:hint="default" /><w:lang w:eastAsia="zh-Hans" /></w:rPr><w:t>。</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="11906" w:h="16838" /><w:pgMar w:top="1440" w:right="1800" w:bottom="1440" w:left="1800" w:header="851" w:footer="992" w:gutter="0" /><w:cols w:space="425" w:num="1" /><w:docGrid w:type="lines" w:linePitch="312" w:charSpace="0" /></w:sectPr></w:body>
</w:document>

6、转化方法

import java.io.*;
import java.util.Enumeration;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import freemarker.template.Configuration;
import freemarker.template.Template;public class XmlToWord {/*** xml转word* @param templatePath 模板目录* @param templateName 模板名称,不带扩展名* @param targetPath 目标文件目录* @param targetName 目标文件名称,不带扩展名* @param dataMap 替换数据*/public static void  xmlToWord(String templatePath, String templateName, String targetPath, String targetName, Map<String,Object> dataMap){try {Configuration configuration = new Configuration();configuration.setDefaultEncoding("UTF-8");//.xml 模板文件所在目录configuration.setDirectoryForTemplateLoading(new File(templatePath));// 输出文档路径及名称 临时文件String tempXmlFilePath = targetPath + "/" + targetName + "_temp.xml";File outTempFile = new File(tempXmlFilePath);if(!outTempFile.exists()){File dirFile =new File(outTempFile.getParent());if(!dirFile.exists()||!dirFile.isDirectory()){dirFile.mkdirs();}}// 以utf-8的编码读取模板文件String xmlName = templateName + ".xml";Template t =  configuration.getTemplate(xmlName,"UTF-8");Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outTempFile), "UTF-8"),10240);t.process(dataMap, out);out.close();File file = new File(tempXmlFilePath);String templateWordPath = templatePath + "/" + templateName + ".docx";File docxFile = new File(templateWordPath);ZipFile zipFile = new ZipFile(docxFile);Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(targetPath + "/" + targetName + ".docx"));int len = -1;byte[] buffer = new byte[1024];while (zipEntrys.hasMoreElements()) {ZipEntry next = zipEntrys.nextElement();InputStream is = zipFile.getInputStream(next);// 把输入流的文件传到输出流中 如果是word/document.xml由我们输入zipout.putNextEntry(new ZipEntry(next.toString()));if ("word/document.xml".equals(next.toString())) {InputStream in = new FileInputStream(file);while ((len = in.read(buffer)) != -1) {zipout.write(buffer, 0, len);}in.close();} else {while ((len = is.read(buffer)) != -1) {zipout.write(buffer, 0, len);}is.close();}}zipout.close();//删除临时文件file.delete();System.out.println("生成成功");}catch (Exception e){e.printStackTrace();System.out.println("生成失败"+e.getMessage());}}}

END

使用xml模板生成word文档相关推荐

  1. JAVA实现模板word文档导入,Java依据word模板生成word文档之后台解析和实现及部分代码(一)...

    Java根据word模板生成word文档之后台解析和实现及部分代码(一) 后台主要工作是解析XML定义的标签文件,并获取到数据集,放入到Map中,然后调用Jacob.jar中提供的相关方法来实现替换. ...

  2. 使用word模板生成word文档的各类方案

    使用word模板生成word文档的各类方案 生成word的各种方案 word另存xml进行后续处理 2003版本word(.doc)的xml处理并生成word 2007版本word(.docx)的xm ...

  3. freemarker根据模板生成word文档,换行

    freemarker根据模板生成word文档,其它地方已经说的非常清除了,在此简单再说以下. 1.制作word模板,另存为xml文件.在此我另存为的时windows xml,它和windows 200 ...

  4. Android 使用模板生成Word文档,支持手机直接查看word

    最近在项目工作中,碰到一个很棘手的需求,说是要在手机端根据模板生成word文档,而且不借助第三方的软件可以查看word文档,一开始听这个需求差不多蒙了,这要怎么做,为什么不把生成word文档这个工作放 ...

  5. 根据标准word模板生成word文档类库(开源)

    前言   最近因项目需要要自定义标准word模板,并以编码方式操作word模板.填充数据和生成word文档,于是自己写了条小"内裤"来实现这个功能.该"内裤"只 ...

  6. PHP 使用word模板生成word文档示例

    <?php namespace Home\Controller; use PhpOffice\PhpWord\TemplateProcessor; use Think\Controller; c ...

  7. 如何使用word模板生成word文档(文本,图片)

    注意:只针对数据信息与图片信息进行生成. 一,准备工作 1,编辑word模板,变量信息以${变量名称}表示,图片要使用临时图片占用位置. 2,转换格式word输出为Word.xml文档格式,在手动改为 ...

  8. 根据模板生成word文档下载

    需求:根据数据库已有字段,填入写好的word并下载 工具 :freemark   一种方便生成word的引擎,内置好大量基础方法 思路: 一.将数据库数据按需求(根据id,根据name......)提 ...

  9. Java根据word模板生成word文档并转成PDF文件

    1. 处理word模板 1.1 定义word模版 1.2 定义完我们的模板之后,我们要将文档保存为xml的格式 定义完我们的模板之后,我们要将文档保存为xml的格式 1.3 xml格式化 生成的xml ...

最新文章

  1. Learn OpenGL (十):材质
  2. 高度平衡二叉树的构建_平衡二叉树(AVL)树
  3. 【好书试读】支付宝体验设计精髓
  4. ajax实现动态及时刷新表格数据_如何爬取网页数据
  5. Sql server 获得某一部门下的所有子部门。根据子部门获得它的上级部门。
  6. 9款jQuery插件为你的网站增加亮点
  7. 【MySQL经典案例分析】关于数据行溢出由浅至深的探讨
  8. JSON数组转LIST集合的两种方法
  9. 小米笔记本Pro黑苹果10.15.2不需要外焊USB接蓝牙,完美支持airdrop、接力、随航
  10. 《辛雷学习方法》读书笔记——前言
  11. sas table将缺失值计入百分比_逻辑回归的评分卡的SAS实现
  12. 基于BPM(业务流程管理)的低代码开发平台有哪些优势?
  13. oracle语句怎么查工作日,SQL查询工作日 - Oracle开发 - ITPUB论坛-中国专业的IT技术社区...
  14. BT5在虚拟机下无法上网
  15. 记一个chrome自带input:-internal-autofill-selected背景色样式问题
  16. Datawhale学习 - 天池大赛比赛全流程体验
  17. 在IT行业中,程序员的学历重要吗?
  18. 带联网功能的RFID宿舍门禁(六)-两年后的再次总结
  19. 编辑为什么建议转投_编辑建议转投其他期刊一般有哪些原因
  20. 谷歌与Oracle:Java版权大战现在牵扯到美国总统奥巴马

热门文章

  1. UEFI中的Protocol浅谈
  2. python提高效率(优化)的心得总结
  3. 安装solidworks 显示:Flexnet Licence Manager is not successfully installed as a service in your system
  4. 解读ICLR 2021:DoodlerGAN创意草图开山之作
  5. linux io平均等待时间计算公式详解,Linux性能定位详解
  6. NLP项目流程及思考逻辑
  7. 背景调查市场现状及未来发展趋势
  8. [c#]通过一个枚举值获取另一个枚举值———通过国家全称获取国家简称
  9. 关于微信小程序的坑uploadFile:fail Error: unable to verify the first certificate
  10. Andriod studio学习 之 对话框