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. 发布一个嘿嘿嘿的技术方案 —— 商用群发p2p网络
  2. 「蚂蚁呀嘿」克星来了!中科院23岁博士生开发「听音识人」,准确率近90%
  3. linux 脚本案例,30个关于Shell脚本的经典案例(上)
  4. nginx自定义http头
  5. 递归和非递归实现二叉排序树(BST)的查找操作
  6. commit git idea 速度慢_关于Git,这篇文章还不够吗?
  7. 信息学奥赛一本通(1053:最大数输出)
  8. HDOJ 2050 折线分割平面
  9. QThread实现多线程
  10. webserver总结
  11. VC2015解决方案管视图中没有外部依赖项、头文件、源文件、资源文件,提供一个本人解决的办法以及总结网上零零散散的方法给后来者提供一个参考
  12. 永恒之蓝(MS17010)漏洞复现
  13. java入门笔记——老王笔记--IT隐匿者
  14. 紧急重要四象限软件用哪款便签软件?
  15. docker 安装 es + kibana + ik + 拼音
  16. easyui树mysql_Easyui 创建异步树形菜单_EasyUI 教程
  17. App测试中有哪些常见的性能测试指标?出具App测试报告的软件测试机构推荐
  18. 最小化函数minimize
  19. StudyJams-第01课_初识Android的View(TextView、ImageView、Button)
  20. 什么是Tuscany?

热门文章

  1. 实验二 配置Trunk和链路汇聚
  2. 数据分析思维之从整体出发分析零售行业——全方位多方面细节分析
  3. 恭喜龙蜥获得中国开源云联盟2022年度中国“最佳开源实践案例”和“杰出开源贡献者”奖项
  4. 华为推送新利器,完美契合用户体验
  5. Unity上传文件(How to upload file to server by unity)
  6. H5 移动端文件下载的方法
  7. qt-opencv图像分割之Otsu算法实例
  8. 如何创新并发出发明专利——以毕设设计电路为例
  9. 使用JDBC完成数据的增加
  10. 计算机设计大赛人工智能挑战赛填写模板