word生成产生错误的原因
技术背景
很多时候我们网站或者系统需要提供一些word文件,例如证明.docx或者订单.docx等文件供用户下载打印等。
用Java操作word文档,毫无疑问,当下最流行apache poi,对于poi如何操作word文档,这里不作过多介绍。
这里主要讲解如何通过一个制作好的word模板文件,通过数据填充,生成加工好的word文件。
官方网站
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"><</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">></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"><</span>String<span class="token punctuation">,</span> Object<span class="token punctuation">></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"><</span>TextRenderData<span class="token punctuation">></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"><</span>RenderData<span class="token punctuation">></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"><</span>Object<span class="token punctuation">></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生成产生错误的原因的更多相关文章
- ORA-01591错误的原因和处理方法
http://blog.csdn.net/tclcaojun/article/details/6777022错误代码:ORA-01591 错误原因:使用了分布式事务,造成这个问题的原因很多时候都是由于 ...
- 出现 java.lang.OutOfMemoryError: PermGen space 错误的原因及解决方法
一.原因及解决方法[1] 1.原因:堆内存的永久保存去区内存分配不足(缺省默认为64M),导致内存溢出错误. 2.解决方法:重新分配内存大小,-Xms1024M -Xmx2048M -XX:PermS ...
- qt 旧项目编译运行提示 “启动程序失败,路径或者权限错误?” 原因及解决方法
qt 旧项目编译运行提示 "启动程序失败,路径或者权限错误?" 原因及解决方法 原因 Qt Creator在打开项目文件的同时会生成.pro.user文件,.pro.user文件叫 ...
- tomcat项目迁移,无法访问,报“404”错误,原因分析
首先,导出项目文件和MySQL数据库(项目文件直接tar&&cp:数据库直接mysqldump生成sql文件) 再,进行导入步骤,项目文件拷贝到webapps下,并赋予bin相关文件执 ...
- ERR_CONTENT_DECODING_FAILED错误的原因和解决办法
1. ERR_CONTENT_DECODING_FAILED错误的原因 这种错误通常发生于Http请求中的头部信息标识内容是gzip编码的,但实际上不是. 2. ERR_CONTENT_DECODIN ...
- The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value. 错误的原因及解决方案
异常描述: 数据访问用EF,在数据库中用getdate()设置的默认值,程序中没有赋值. 出现异常. 此错误在百度上在我写此文之前没有多少解决方案,谷歌之等到以下两个有用的页: http://stac ...
- [转载]触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因
触发ASSERT(afxCurrentResourceHandle != NULL)错误的原因 Debug Assert error afxwin1.inl line:22 翻译参考 http://w ...
- Aspose.Words操作word生成PDF文档
Aspose.Words操作word生成PDF文档 using Aspose.Words; using System; using System.Collections.Generic; using ...
- Nginx 499错误的原因及解决方法
今天进行系统维护,发现了大量的499错误, 499错误 ngx_string(ngx_http_error_495_page), /* 495, https certificate error */n ...
- [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
[转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法 http://blog.csdn.net/sahuso ...
随机推荐
- angular 输入框获取焦点失效的问题
重点说明: 假如先设定了 输入框disabled又打开,然后执行获取焦点,可能会失效,这个时候可以把getFocus函数放在定时器里,1秒钟后执行,就可以了哦~ 具体代码如下 html ...
- file过滤器的原理和使用-FileNameFilter过滤器的使用和lambda优化程序
file过滤器的原理和使用 java.io.FileFilter是一个接口,是File的过滤器.该接口的对象可以传递给File类的listFiles(FileFilter)作为参数,接口中只有一个方法 ...
- MRS_Debug仿真相关问题汇总
解决问题如下: Debug时,看不到外设寄存器选项 Debug时,更改变量显示类型 Debug时,断点异常 跳过所有断点 取消仿真前自动下载程序 Debug时仅擦除程序代码部分flash空间 保存De ...
- P15_了解小程序的版本阶段与上线的主要步骤
协同工作和发布 - 小程序的版本 软件开发过程中的不同版本 在软件开发过程中,根据时间节点的不同,会产出不同的软件版本,例如: 开发者编写代码的同时,对项目代码进行自测(开发版本) 直到程序达到一个稳 ...
- npm : 无法将“npm”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
出现问题原因: vscode运行前端命令,没有为安装的npm配置环境变量 解决办法: 配置环境变量,可查看前一篇文章nodejs 环境变量配置 配置完后还出如题原因: 重新启动vscode终端powe ...
- Error: EPERM: operation not permitted, mkdir ‘C:\Program Files\nodejs‘TypeError: Cannot read proper
出现问题: 问题如题,出现场景:vscode运行npm命令 解决办法: 有的友友说安装nodejs时用管理员身份安装,右键没找到最后删掉了此文件即可. 这个文件缓存了之前的配置与现在安装的nodejs ...
- fast planner总结
一.前端 kinodynamic A*算法动力学路径搜索 1.1 路径搜索的主要函数为kinodynamicAstar类的search函数 int KinodynamicAstar::search(E ...
- Networking && Internet 计网学习笔记一
Networking && Internet 计网学习笔记一 参考书籍: James F. Kurose, Keith W. Ross. 计算机网络-自顶向下方法 (7th). 机械工 ...
- javaEE(网络编程、TCP、线程池优化)
网络编程 Client-Server(CS) Browser/Server(BS) 1.客户端--服务端 安装客户端 更新. 依赖PC 2.浏览器和服务端 分布式 兼容性 一站开发 网络通信: UDP ...
- 都在用 AI 生成美少女,而我却。。。
最近 AI 画画特别的火,你能从网上看到非常多好看的图片,于是我就开始了我的安装之旅,我看到的图是这样的. 这样的. 还有这样的. 然后我就开始了我的 AI 安装生成计划. 安装环境 首先我们需要安装 ...