技术背景

很多时候我们网站或者系统需要提供一些word文件,例如证明.docx或者订单.docx等文件供用户下载打印等。

用Java操作word文档,毫无疑问,当下最流行apache poi,对于poi如何操作word文档,这里不作过多介绍。

这里主要讲解如何通过一个制作好的word模板文件,通过数据填充,生成加工好的word文件。

官方网站

http://deepoove.com/poi-tl

Maven

<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.3.1</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

实战内容


以下源码已经上传再我的开源项目里面的spring-cloud-study-poi
https://github.com/moshowgame/spring-cloud-study

说先是新建模板word文档(。docx格式,2007/2013/2016新格式的)

运行效果

核心实现代码


@RestController
public class PoiController {
<span class="token annotation punctuation">@GetMapping</span><span class="token punctuation">(</span><span class="token string">"/generateWord"</span><span class="token punctuation">)</span>
<span class="token keyword">public</span> ApiReturnObject <span class="token function">generateWord</span><span class="token punctuation">(</span>String data<span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span>
<span class="token comment">//图片路径,请注意你是linux还是windows</span>
String wordPath<span class="token operator">=</span><span class="token string">"C:\\Users\\Administrator\\Desktop\\"</span><span class="token punctuation">;</span>
String modelName<span class="token operator">=</span><span class="token string">"人物模板.docx"</span><span class="token punctuation">;</span>
String outputName<span class="token operator">=</span><span class="token string">"hanyunxi.docx"</span><span class="token punctuation">;</span>
Map<span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">&gt;</span></span> datas <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics function"><span class="token punctuation">&lt;</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
<span class="token punctuation">{<!-- --></span>
<span class="token comment">//本地图片</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"name"</span><span class="token punctuation">,</span><span class="token string">"鞠婧祎"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"team"</span><span class="token punctuation">,</span><span class="token string">"前SNH48"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"gender"</span><span class="token punctuation">,</span><span class="token string">"女"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"birthday"</span><span class="token punctuation">,</span><span class="token string">"19940618"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"remark"</span><span class="token punctuation">,</span><span class="token string">"鞠婧祎,1994年6月18日出生于四川遂宁,毕业于四川音乐学院附属中学,中国女演员、歌手,原SNH48 TEAM NII成员[1]。2013年9月5日,升格为SNH48 TEAM NII二期生正式成员;11月2日,以《剧场女神》公演正式出道。2014年6月7日,拍摄个人首支MV《足球派对》;7月26日,参加SNH48“一心向前”演唱会暨SNH48第一届总选举,演唱《流着泪微笑》并获SNH48总选举第四名。2015年1月15日,发行出道两周年EP《青春的约定》。2016年12月10日,获“星光大赏”年度新锐电视剧女演员。2017年1月1日,参加安徽卫视《国剧盛典》;3月27日,获第24届东方风云榜音乐盛典“年度飞跃歌手”奖[2];5月4日,获团中央“五四优秀青年”称号;5月25日,发行第二张个人EP《等不到你》;[3]7月29日,参加“我心翱翔”第四届总选举发布演唱会,获得SNH48第四届总选举第1名;[4]12月15日,SNH48官方宣布,鞠婧祎正式从SNH48 Group单飞、成立个人工作室。2018年3月26日,获第25届《东方风云榜》音乐盛典年度跨界艺人奖。7月28日,参加湖南卫视综艺节目《快乐大本营》。"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"active"</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">NumbericRenderData</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation">&lt;</span>TextRenderData<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TextRenderData</span><span class="token punctuation">(</span><span class="token string">"FF00FF"</span><span class="token punctuation">,</span> <span class="token string">"2013年 以《剧场女神》公演正式出道"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TextRenderData</span><span class="token punctuation">(</span><span class="token string">"FF00FF"</span><span class="token punctuation">,</span> <span class="token string">"2014年 拍摄个人首支MV《足球派对》"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TextRenderData</span><span class="token punctuation">(</span><span class="token string">"FF00FF"</span><span class="token punctuation">,</span> <span class="token string">"2015年 发行出道两周年EP《青春的约定》"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TextRenderData</span><span class="token punctuation">(</span><span class="token string">"FF00FF"</span><span class="token punctuation">,</span> <span class="token string">"2016年 主演玄幻剧《九州天空城》"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"tables"</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">TableRenderData</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation">&lt;</span>RenderData<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TextRenderData</span><span class="token punctuation">(</span><span class="token string">"d0d0d0"</span><span class="token punctuation">,</span> <span class="token string">"节目"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TextRenderData</span><span class="token punctuation">(</span><span class="token string">"d0d0d0"</span><span class="token punctuation">,</span> <span class="token string">"次数"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics function"><span class="token punctuation">&lt;</span>Object<span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span><span class="token punctuation">{<!-- --></span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"《SNH星剧院公演》;999"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"《敢ZUO敢为女声秀》;4"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">add</span><span class="token punctuation">(</span><span class="token string">"《快乐大本营》;2"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string">"no datas"</span><span class="token punctuation">,</span> <span class="token number">10600</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

// //网路图片

put("picture", new PictureRenderData(200, 250, ".png", BytePictureUtils.getUrlByteArray("https://pic.baike.soso.com/ugc/baikepic2/18293/cut-20170602162513-2088410512.jpg/300")));

			<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span> XWPFTemplate template <span class="token operator">=</span> XWPFTemplate<span class="token punctuation">.</span><span class="token function">compile</span><span class="token punctuation">(</span>wordPath<span class="token operator">+</span>modelName<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span>datas<span class="token punctuation">)</span><span class="token punctuation">;</span>
FileOutputStream out<span class="token punctuation">;</span>
<span class="token keyword">try</span> <span class="token punctuation">{<!-- --></span>
out <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">FileOutputStream</span><span class="token punctuation">(</span>wordPath<span class="token operator">+</span>outputName<span class="token punctuation">)</span><span class="token punctuation">;</span>
template<span class="token punctuation">.</span><span class="token function">write</span><span class="token punctuation">(</span>out<span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
out<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
template<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">FileNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{<!-- --></span>
e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> <span class="token keyword">return</span> ApiReturnUtil<span class="token punctuation">.</span><span class="token function">success</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58

支持的模板标签

  • 文本模板{{var}}
put("author", new TextRenderData("000000", "Sayi卅一"));
put("introduce", "http://www.deepoove.com");
  • 1
  • 2
  • 图片模板{{@var}}
// 本地图片
put("localPicture", new PictureRenderData(120, 120, "./sayi.png")); // 本地图片byte数据

byte[] localByteArray = BytePictureUtils.getLocalByteArray(new File("./logo.png"));

put("localBytePicture", new PictureRenderData(100, 120, ".png", localByteArray)); // 网络图片

put("urlPicture", new PictureRenderData(100, 100, ".png", BytePictureUtils.getUrlByteArray("https://avatars3.githubusercontent.com/u/1394854"))); // java 图片

put("bufferImagePicture", new PictureRenderData(100, 120, ".png", BytePictureUtils.getBufferByteArray(bufferImage)));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 表格模板{{#var}}
put("changeLog", new TableRenderData(new ArrayList<RenderData>(){{
add(new TextRenderData("d0d0d0", ""));
add(new TextRenderData("d0d0d0", "introduce"));
}},new ArrayList<Object>(){{
add("1;add new # gramer");
add("2;support insert table");
add("3;support more style");
}}, "no datas", 10600));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 列表模板{{*var}}
put("feature", new NumbericRenderData(new ArrayList<TextRenderData>() {
{
add(new TextRenderData("Plug-in grammar"));
add(new TextRenderData("Supports word text, header..."));
add(new TextRenderData("Not just templates, but also style templates"));
}
}));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 文档模板{{+var}}
List<SegmentData> segments = new ArrayList<SegmentData>();
SegmentData s1 = new SegmentData();
s1.setTitle("经常抱怨的自己");
s1.setContent("每个人生活得都不容易,经常向别人抱怨的人,说白了就是把对方当做“垃圾场”,你一股脑地将自己的埋怨与不满倒给别人,自己倒是爽了,你有考虑过对方的感受吗?对方的脸上可能一笑了之,但是心里可能有一万只草泥马奔腾而过。");
segments.add(s1); SegmentData s2 = new SegmentData();

s2.setTitle("拖拖拉拉的自己");

s2.setContent("能够今天做完的事情,不要拖到明天,你的事情没有任何人有义务去帮你做;不要做“宅男”、不要当“宅女”,放假的日子约上三五好友出去转转;经常动手做家务,既能分担伴侣的负担,又有一个干净舒适的环境何乐而不为呢?");

segments.add(s2); put("docx_word", new DocxRenderData(new File("~/segment.docx"), segments));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

可能的报错

如果出现这个错误,要判断文件后缀名是doc,还是docx。
对POI来说,如果是doc,使用HSSFWorkbook;如果是docx,使用XSSFWorkbook。
而POI-TL封装的是docx的版本,现在是主流格式,所以推荐用这个,当然,输出文件随意,你输出文件名弄成doc也ok(格式)

org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException: The supplied data appears to be in the OLE2 Format. You are calling the part of POI that deals with OOXML (Office Open XML) Documents. You need to call a different part of POI to process this data (eg HSSF instead of XSSF)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:184)
at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:241)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:98)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:116)
at com.deepoove.poi.NiceXWPFDocument.<init>(NiceXWPFDocument.java:85)
at com.deepoove.poi.XWPFTemplate.compile(XWPFTemplate.java:129)
at com.deepoove.poi.XWPFTemplate.compile(XWPFTemplate.java:110)
at com.deepoove.poi.XWPFTemplate.compile(XWPFTemplate.java:78)
at com.deepoove.poi.XWPFTemplate.compile(XWPFTemplate.java:74)
at com.softdev.system.demo.controller.PoiController.generateWord(PoiController.java:51)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

word生成产生错误的原因的更多相关文章

  1. ORA-01591错误的原因和处理方法

    http://blog.csdn.net/tclcaojun/article/details/6777022错误代码:ORA-01591 错误原因:使用了分布式事务,造成这个问题的原因很多时候都是由于 ...

  2. 出现 java.lang.OutOfMemoryError: PermGen space 错误的原因及解决方法

    一.原因及解决方法[1] 1.原因:堆内存的永久保存去区内存分配不足(缺省默认为64M),导致内存溢出错误. 2.解决方法:重新分配内存大小,-Xms1024M -Xmx2048M -XX:PermS ...

  3. qt 旧项目编译运行提示 “启动程序失败,路径或者权限错误?” 原因及解决方法

    qt 旧项目编译运行提示 "启动程序失败,路径或者权限错误?" 原因及解决方法 原因 Qt Creator在打开项目文件的同时会生成.pro.user文件,.pro.user文件叫 ...

  4. tomcat项目迁移,无法访问,报“404”错误,原因分析

    首先,导出项目文件和MySQL数据库(项目文件直接tar&&cp:数据库直接mysqldump生成sql文件) 再,进行导入步骤,项目文件拷贝到webapps下,并赋予bin相关文件执 ...

  5. ERR_CONTENT_DECODING_FAILED错误的原因和解决办法

    1. ERR_CONTENT_DECODING_FAILED错误的原因 这种错误通常发生于Http请求中的头部信息标识内容是gzip编码的,但实际上不是. 2. ERR_CONTENT_DECODIN ...

  6. The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 错误的原因及解决方案

    异常描述: 数据访问用EF,在数据库中用getdate()设置的默认值,程序中没有赋值. 出现异常. 此错误在百度上在我写此文之前没有多少解决方案,谷歌之等到以下两个有用的页: http://stac ...

  7. [转载]触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因

    触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因 Debug Assert error afxwin1.inl line:22 翻译参考 http://w ...

  8. Aspose.Words操作word生成PDF文档

    Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...

  9. Nginx 499错误的原因及解决方法

    今天进行系统维护,发现了大量的499错误, 499错误 ngx_string(ngx_http_error_495_page), /* 495, https certificate error */n ...

  10. [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法

    [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法 http://blog.csdn.net/sahuso ...

随机推荐

  1. 看不懂打我系列------图文并茂基于CentOS Linux release 7.8.2003 Core安装并Docker化你的Node.js应用

    @图文并茂基于CentOS Linux release 7.8.2003 Core安装并Docker化你的Node.js应用 简体中文 | English 说明 本文介绍如何在CentOS Linux ...

  2. 宇宙无敌搞笑轻松弄懂java动态代理

    https://www.cnblogs.com/ferryman/p/13170057.html jdk动态代理和cglib动态代理区别 https://blog.csdn.net/shallynev ...

  3. Java 入门与进阶P-7.3+P-7.4

    函数的调用 简单应用举例 例1:编写一个求两个数的和的子函数 要求使用键盘录入的方式: 分析与总结: 要实现某一功能,把其定义为一个函数封装起来 就可以重复多次使用:这样一来,main函数里的代码减少 ...

  4. 06yarn简介

    YARN简介 一.YARN是什么 YARN不是facebook的那个yarn,它从Hadoop 2引入,最初目的是改善MapReduce的实现,但是因为具备足够通用性,同样也可以支持其他的分布式计算模 ...

  5. java应用定位高cpu占用几步操作

    1.top获取高cpu占用的pid,如266202.查看pid的线程情况, top -H -p 266203.把cpu高占用的线程号转为16进制,printf "%x" 26652 ...

  6. Django-request、django连接数据库、ORM

    1.静态文件配置 1.静态文件:不经常变化的文件,主要针对html文件(CSS文件.js文件.img文件.第三方框架文件). 2.django针对静态文件资源需要单独开始一个目录统一存放:static ...

  7. win10使用python自动化安装mysql8.0.11

    流程概要 下载mysql-8.0.11-winx64压缩包 解压 编辑配置文件my.ini 管理员权限cmd安装(注意初始化时设置默认密码为空) pymysql连接,执行sql操作. 代码实现 依赖: ...

  8. Svelte框架结合SpreadJS实现表格协同文档

    SpreadJS是葡萄城结合 40 余年专业控件技术和在电子表格应用领域的经验而推出的纯前端表格控件.作为一个类Excel控件,SpreadJS如何实现当前比较流行的表格协同呢?本篇文章将简单介绍一下 ...

  9. .net 多地点计算中心点

    1.需求产生 快到周末了,几个远在各个区的朋友想要聚餐,为了照顾到彼此的距离,决定计算一下所有人的中心点,至此需求产生,下面开始编写代码. 2.编写代码 1)新建一个控制台程序 在NuGet程序包管理 ...

  10. ROS入门:服务

    服务端实现一个加法,将结果反馈给客户端 客户端将要计算的和的两个数发给服务的. 1.自定义服务数据 创建一个srv文件:AddTwoInts.srv int64 a int64 b --- int64 ...