栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

POI-TL的坑 POI重复依赖

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

POI-TL的坑 POI重复依赖

poi-tl(poi template language)是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库。

官方文档:http://deepoove.com/poi-tl/
源码:https://github.com/Sayi/poi-tl

原项目中的maven依赖:



cn.afterturn
easypoi-base
4.3.0


cn.afterturn
easypoi-web
4.3.0


cn.afterturn
easypoi-annotation
4.3.0


org.apache.poi
poi
5.0.0


org.apache.poi
poi-scratchpad
5.0.0


org.apache.poi
poi-ooxml
5.0.0


org.apache.poi
poi-ooxml-schemas
4.1.2



com.deepoove
poi-tl
1.10.3


项目需求需要在导出的word中渲染表格
参考文档表格行循环:http://deepoove.com/poi-tl/#hack-loop-table

word模板格式:

配置{{table}}标签渲染策略:

LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder().bind("table", policy).build();
XWPFTemplate template = XWPFTemplate.compile(resource.getInputStream(), config).render(data);

生成带表格的word过程中,找不到方法

java.lang.NoSuchMethodError: org.openxmlformats.schemas.wordprocessingml.x2006
网上找了一下,各种论坛里面都是这一篇的中文翻译版
https://stackoverflow.com/questions/26854838/java-lang-nosuchmethoderror-org-openxmlformats-schemas-wordprocessingml-x2006-m
就是说 poi-ooxml-schemas 这个依赖是精简版的

poi相关依赖的作用:
https://poi.apache.org/components/index.html

尝试改用ooxml-schemas依赖
结果还是报错(跑偏了)

升级POI版本到5.2.2之后:
遇到了新的问题

Caused by: java.lang.NoSuchFieldError: Factory
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:196)
    at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:169)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.(XWPFDocument.java:159)
    at com.deepoove.poi.xwpf.NiceXWPFDocument.(NiceXWPFDocument.java:118)
    at com.deepoove.poi.xwpf.NiceXWPFDocument.(NiceXWPFDocument.java:114)
    at com.deepoove.poi.XWPFTemplate.compile(XWPFTemplate.java:178)

在poi-tl Git仓库的Issues里面找到了同样的问题,作者回复是依赖冲突
https://github.com/Sayi/poi-tl/issues/737

poi-tl里面点进去可以看到(回到正确的方向了)
其实这个包里面已经包含了poi、poi-ooxml、poi-ooxml-schemas-extra这三个包。
与外面添加的poi相关依赖重复了

去掉多余的依赖,可以正常生成word了!

截取部分日志信息:

标签默认的渲染策略是:TextRenderPolicy
现在给{{table}}配置LoopRowTableRenderPolicy策略,渲染到指定标签时,会去渲染表格循环行的标签 [index]等

2022-05-20 09:47:37.977 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.DefaultRender.render:57 -Render template start...
2022-05-20 09:47:37.984 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.LogProcessor.visit:57 -{{data}}
2022-05-20 09:47:37.986 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.LogProcessor.visit:57 -{{bianShenQian}}
2022-05-20 09:47:37.987 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.LogProcessor.visit:57 -{{table}}
2022-05-20 09:47:37.993 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template {{data}}, Sign: , policy:TextRenderPolicy
2022-05-20 09:47:38.024 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template {{bianShenQian}}, Sign: , policy:TextRenderPolicy
2022-05-20 09:47:38.024 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template {{table}}, Sign: , policy:LoopRowTableRenderPolicy
2022-05-20 09:47:38.028 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.buildRunEdge:218 -[index]
2022-05-20 09:47:38.028 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:259 -[Start]:The run position of [index] is 0, Offset in run is 0
2022-05-20 09:47:38.028 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:260 -[End]:The run position of [index] is 0, Offset in run is 6
2022-05-20 09:47:38.029 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.TemplateResolver.parseTemplateFactory:321 -Resolve where text: [index], and create RunTemplate for XWPFRun
2022-05-20 09:47:38.029 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template [index], Sign: , policy:TextRenderPolicy
2022-05-20 09:47:38.034 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.buildRunEdge:218 -[content]
2022-05-20 09:47:38.035 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:259 -[Start]:The run position of [content] is 0, Offset in run is 0
2022-05-20 09:47:38.035 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:260 -[End]:The run position of [content] is 0, Offset in run is 8
2022-05-20 09:47:38.035 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.TemplateResolver.parseTemplateFactory:321 -Resolve where text: [content], and create RunTemplate for XWPFRun
2022-05-20 09:47:38.035 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template [content], Sign: , policy:TextRenderPolicy
2022-05-20 09:47:38.037 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.buildRunEdge:218 -[index]
2022-05-20 09:47:38.037 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:259 -[Start]:The run position of [index] is 0, Offset in run is 0
2022-05-20 09:47:38.037 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:260 -[End]:The run position of [index] is 0, Offset in run is 6
2022-05-20 09:47:38.037 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.TemplateResolver.parseTemplateFactory:321 -Resolve where text: [index], and create RunTemplate for XWPFRun
2022-05-20 09:47:38.037 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template [index], Sign: , policy:TextRenderPolicy
2022-05-20 09:47:38.040 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.buildRunEdge:218 -[content]
2022-05-20 09:47:38.040 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:259 -[Start]:The run position of [content] is 0, Offset in run is 0
2022-05-20 09:47:38.040 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.RunningRunParagraph.loggerInfo:260 -[End]:The run position of [content] is 0, Offset in run is 8
2022-05-20 09:47:38.040 DEBUG[http-nio-8052-exec-3]com.deepoove.poi.resolver.TemplateResolver.parseTemplateFactory:321 -Resolve where text: [content], and create RunTemplate for XWPFRun
2022-05-20 09:47:38.040 INFO [http-nio-8052-exec-3]com.deepoove.poi.render.processor.ElementProcessor.visit:74 -Start render Template [content], Sign: , policy:TextRenderPolicy

总结:poi-tl依赖里面已经包含了poi相关依赖 无需重复引用!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/918284.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号