生成word思路

用WPS或者office编辑好word的样式,然后另存为word xml文档,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Docx。

编辑好需要使用的word文档

1、把需要注入的信息换成变量名称,比如几年几月用${d1}表示,全部替换后的格式如下图所示

对于表头的话最好设置成每页都自动生成表头

2、替换完成后另存为word xml格式的文档,如下图

3、使用文本编辑器打开

4、xml格式化

https://c.runoob.com/front-end/710/

5、选定表格的动态生成范围,添加 list 标签,记得保存

<#list TestList as item>

</#list>

6、把改好的XML文件存放到项目的resources文件夹下,(我这里模板比较多,就自己建了一个word文件夹)

7,导入依赖

<!-- 导出word -->
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/e-iceblue/spire.doc.free -->
<dependency><groupId>e-iceblue</groupId><artifactId>spire.doc.free</artifactId><version>2.7.3</version>
</dependency>

spire.doc.free这个依赖要在maven的setting配置加个仓库

<repository>
      <id>e-iceblue</id>
      <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
      </repository>

8,编写代码

control类

@GetMapping("/word02")
public void getWord02() throws Exception {//市级开卡数据ArrayList<HashMap<String, String>> list = new ArrayList<>();for (int i = 0; i < 3; i++) {HashMap<String, String> map = new HashMap<>();int a = i + 1;String s = Integer.toString(a);map.put("t1", s);map.put("t2", "666666");map.put("t3", "6666");map.put("t4", "66");map.put("t5", "6666");map.put("t6", "6666");map.put("t7", "66666666");list.add(map);}Map<String, Object> params = new HashMap<>();params.put("d1", "10月");params.put("d2", "666666");params.put("p1", "6666");params.put("p2", "6666");params.put("p3", "222211");params.put("TestList", list);Integer templateFileNumber = 2;WordUtil.createWord(params,templateFileNumber);
}

导出word工具类

/*** 将数据导入word模板生成word** @param params             数据* @param templateFileNumber 1-模板 2-模板 3- 模板* @return docx临时文件的全路径* @throws IOException*/private static String createWord(Map<String, Object> params, Integer templateFileNumber) throws IOException {String templateFile = null;//指定模板if (templateFileNumber == 1) {templateFile = "啊啊啊.xml";}if (templateFileNumber == 2) {templateFile = "哦哦哦.xml";}if (templateFileNumber == 3) {templateFile = "嗯嗯嗯.xml";}File file = null, templateFile1 = null;FileOutputStream fileOutputStream = null;Writer out = null;InputStream inputStream = null;try {//指定模板存放位置ClassPathResource tempFileResource = new ClassPathResource("word/" + templateFile);//创建临时文件file = File.createTempFile(templateFile, ".docx");//得到临时文件全路径  ,作为word生成的输出全路径使用String outputDir = file.getAbsolutePath();log.info("创建临时文件的路径为:{}", outputDir);//创建模板临时文件templateFile1 = File.createTempFile(templateFile, ".xml");fileOutputStream = new FileOutputStream(templateFile1);inputStream = tempFileResource.getInputStream();IOUtils.copy(inputStream, fileOutputStream);//得到临时模板文件全路径String templateFilePath = templateFile1.getAbsolutePath();log.info("创建临时文件的路径为:{}", templateFilePath);//           new ClassPathResource("aaa").getInputStream().close();// 设置FreeMarker的版本和编码格式Configuration configuration = new Configuration();configuration.setDefaultEncoding("UTF-8");// 设置FreeMarker生成Word文档所需要的模板的路径configuration.setDirectoryForTemplateLoading(templateFile1.getParentFile());// 设置FreeMarker生成Word文档所需要的模板Template t = configuration.getTemplate(templateFile1.getName(), "UTF-8");// 创建一个Word文档的输出流out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(outputDir)), "UTF-8"));//FreeMarker使用Word模板和数据生成Word文档t.process(params, out); //将填充数据填入模板文件并输出到目标文件} catch (Exception e) {log.error("word生成报错,错误信息{}", e.getMessage());e.printStackTrace();} finally {if (out != null) {out.flush();out.close();}if (inputStream != null) {inputStream.close();}if (fileOutputStream != null) {fileOutputStream.close();}if (templateFile1 != null) {templateFile1.delete();}}return file == null ? null : file.getAbsolutePath();}

最终会得到docx文件!!!!

java用模板生成word(docx)文档(含动态表格)相关推荐

  1. word模板生成word报表文档

    主要功能为根据word模板生成word报表文档,注意引用Interop.Word.dll; 首先要生成word程序对象 Word.Application app = new Word.Applicat ...

  2. Java利用poi生成word(包含插入图片,动态表格,行合并)

    Java利用poi生成word(包含插入图片,动态表格,行合并) 测试模板样式: 图表 1 Word生成结果: 图表 2 需要的jar包:(具体jar可自行去maven下载) Test测试类: imp ...

  3. python生成word目录_使用Python更新MS Word .docx文档的目录(目录)

    我使用python包" python-docx"来修改MS Word .docx文档的结构和内容.该软件包无法更新TOC(目录)[Python: Create a "Ta ...

  4. 针对word.docx文档的关键词索引器

    前言 之前曾想做这样一个工具,用来遍历目录下的word.docx文档查询关键词,主要是有几个目的: 1.在项目开始阶段,通过关键词检索feature,我脑子实在是记不住文档都在哪: 2.收尾阶段,检查 ...

  5. java根据模板生成word文档_Python办公自动化:使用python来自动生成word文档

    让python做办公自动化,让你闲下来 让python做自动化,让你闲下来 上节对python的excel Python办公自动化系列:自动操作Excel自动化做了介绍.这次介绍如何用python对w ...

  6. Java web/springboot上传word/doc/docx文档(含图片)与HTML富文本导入/导出互相转换解析!附项目源码

    测试效果 先看下效果 文档内容如下: 上传 上传docx文档 查看解析内容 <html><head><style>p{margin-top:0pt;margin-b ...

  7. pythondocx模板_python操作docx文档(转)

    关于python操作docx格式文档,我用到了两个python包,一个便是python-docx包,另一个便是python-docx-template;,同时我也用到了很出名的一个工具"pa ...

  8. python 操作 word 文档,使用 python-docx 操作 word docx 文档

    写在前面 python-docx 不支持 doc 文档,一定要注意该点,如果使用 doc 文档,需要提前将其用 Word 相关软件转换为 docx 格式. doc 和 docx 是存在本质差异的,一个 ...

  9. poi操作word docx文档内容替换详解

    WORD文档文字替换 利用关键字下角标位置的方式处理word文档区域文字分割无法匹配的问题 /*** DOCX文档字符串查找** @param document 文档* @param str 查找字符 ...

最新文章

  1. Google智能生态链的演进路径
  2. 大数据认证为什么学python_大数据为什么需要学python?
  3. 文档服务器 件排名,服务器排名
  4. apache、nignx等日志分析工具
  5. 股票软件开发中全推与点播的区别
  6. Halcon 一维测量
  7. QUIC/UDT/SRT
  8. 2017乌鲁木齐ICPC: I. A Possible Tree(带权并查集)
  9. 什么是BI(Business Intelligence
  10. 概率论:p(x|theta)和p(x;theta)的区别
  11. 股票群的骗术 ,几乎99%的QQ群
  12. 055 集体照 (25 分)
  13. matlab p值 z值,显著性水平 p值 z值
  14. 零基础学习编程大概需要多久?
  15. 性能分析之响应时间拆分及 258 原则误区
  16. Unity 基础 之 在 UGUI 上简单实现VideoPlayer视频播放的功能,简单暂停播放/显示视频名称/显示时长/拖拽播放等
  17. Linux ACLs
  18. 柠檬班性能测试day05-0526-04环境安装配置
  19. Swift减少print函数的耗费
  20. 学如逆水行舟,不进则退。

热门文章

  1. BI平台能做什么,有哪些功能呢?
  2. mysql qc_MySQL数据库编程中QC的使用方法
  3. android widget 点击事件,Android Widget点击事件
  4. 魔坊APP项目-17-种植园,商城页面、服务端提供商品api,解决App打包编译以后的跨域限制、客户端获取商品列表并进行展示,集成Alipayplus模块完成支付
  5. Esper学习之九:EPL语法(五)
  6. 《BREW进阶与精通——3G移动增值业务的运营、定制与开发》连载之9---移动增值业务产业链
  7. 编写完10万行代码,我发了篇长文吐槽Rust
  8. Android 广播(Android 10)
  9. Webcam with JavaFX
  10. 鸿蒙系统一键安装,华为鸿蒙系统支持第三方手机,完全开源开放毫无保留