关于java使用POI导出ppt ,其中表格setText 失败问题
1、导出ppt 必要的包
使用maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
2、poi-ooxml-schemas 和 ooxml-schemas 的区别
http://blog.sina.com.cn/s/blog_801b121d0102x3fa.html
3、应用
3.1、表格操作
public void parsing(XSLFSlide slide, XSLFTable table) {
//for循环增加数据,忽略标题行
List<XSLFTableRow> tableRow = table.getRows();
List<XSLFTableCell> cells = tableRow.get(1).getCells();
while(table.getRows().size()<tableData.getRows()){
XSLFTableRow row = table.addRow();
for (int i1 = 0; i1 < cells.size(); i1++) {
row.addCell();
}
}
for (int i = 0; i < tableData.getRows(); i++) {
for (int i1 = 0; i1 < tableData.get(i).size(); i1++) {
String key = tableData.get(i).get(i1);
if(table.getRows().get(i + 1).getCells().size()<i1+1){
table.getRows().get(i + 1).addCell();
}
table.getRows().get(i + 1).getCells().get(i1).setText(key);
}
}
}
3.2、替换图片操作
插入图片自己查资料,这里不提
这里只说读取模板ppt,并替换特定区域成图片。
if(shape[i] instanceof XSLFAutoShape){
XSLFAutoShape txShape = (XSLFAutoShape) shape[i];
if (txShape.getText().contains("{pic}")) {
byte[] pictureData = new byte[0];
try {
if(imageIndex>inputStreamList.size()-1){
continue;
}
pictureData = IOUtils.toByteArray(inputStreamList.get(imageIndex++));
} catch (IOException e) {
e.printStackTrace();
}
int idx = slide.getSlideShow().addPicture(pictureData, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape pic = slide.createPicture(idx);
// 设置XSLFPictureShape的位置信息
pic.setAnchor(txShape.getAnchor());
// 移除XSLFTextShape
slide.removeShape(txShape);
}
其中 imageIndex 表示我图片流的位置,因为我兼容多张图片替换,所以根据查找到的图形的,依次替换。
XSLFAutoShape 表示,图形图片,例如以下我使用的图片,中间{pic}表示我要替换成图片的意思,详细看代码。

3.3、文本操作(略过)
4、问题汇总
4.1、表格 setText 失败问题
可能是因为 poi-ooxml-schemas.jar 包,出现bug 的问题。4.0版本以下poi-ooxml-schemas都出现setText 失败问题。4.0版本以上 可能是修复好了。
4.1.1、解决方案有两种:
① 使用poi-ooxml-schemas 4.0以上版本
② 使用ooxml-schemas 版本,移除 poi-ooxml-schemas 版本 ,两个区别可以参考 第2标题。
4.2、读取*.ppt失败
XMLSlideShow slideShow = new XMLSlideShow(); //只支持读取pptx 文件。
4.3、关于setText 后文字样式失效问题
解决方法:可以使用 把他转成xmlString ,并修改,修改后并重新set 进去
private void setTextBox(XSLFTextBox textbox) {
String xml = textbox.getXmlObject().xmlText();
// 使用freemarker 把xml的${...}数据替换,并返回替换后数据
String tx =parsingText(xml,data);
CTTableCell ctTableCell = null;
try {
//重新把xml set进去该tablecell
ctTableCell = CTTableCell.Factory.parse(tx);
textbox.getXmlObject().set(ctTableCell);
} catch (XmlException e) {
e.printStackTrace();
}
}
5、文档资料
[poi 官网] http://poi.apache.org/
[poi 操作office说明] http://poi.apache.org/components/index.html
[git项目]:https://code.aliyun.com/1003771635/document.git
在子项目 pptXSLF 中。
6、个人说明
如果你们发现其他问题或者解决方法,可以在评论区里提出。
关于java使用POI导出ppt ,其中表格setText 失败问题的更多相关文章
- java使用poi读取ppt文件和poi读取excel、word示例
java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm
- Java之POI导出Excel(一):单sheet
相信在大部分的web项目中都会有导出导入Excel的需求,今天我们就来看看如何用Java代码去实现 用POI导出Excel表格. 一.pom引用 pom文件中,添加以下依赖 查看代码 <!-- ...
- java利用poi来读取execl表格返回对象
利用poi来读取execl表格,返回一个对象(可能有点不完善,但是应该能满足平常的所用),用到了反射等等; 使用的jar包有: commons-collections4-4.1.jar poi-3.1 ...
- java 使用POI导出百万级数据
先看结果吧,这只是测试其中有很多因数影响了性能. 表总数为:7千多万,测试导出100万 表字段有17个字段 最终excel大小有60多兆 总耗时:126165毫秒 差不多2分多钟 其核心简单来说就是分 ...
- java解决poi导出excel文字水印,导出excel不可操作问题
首先需求是用户提出导出excel数据需使用水印备注其用途: 其实就是在导出excel的同时带有自定义文字水印的导出. 那么我们首先想到的肯定是以一个什么样的思路去解决该问题,首先查找poi导出exce ...
- java使用poi导出excel
继上一篇导出pdf,这篇导出excel. 1.导入依赖 <dependency> <groupId>org.apache.poi</groupId> <art ...
- Java使用POI导出excel(下)——实例与小技巧
[更新]:thinkgem的导出工具类: /** * Copyright © 2012-2016 <a href="https://github.com/thinkgem/jeesit ...
- POI导出PPT
1.null <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <g ...
- JAVA 使用POI导出数据格式为Execl
需要下载一个poi的jar包. 控制器 @Override public void getContractListExecl(Contract contract, BindingResult resu ...
随机推荐
- maya模板lock工具
#lockTemple import maya.cmds as mcimport stringif mc.window('LockWin',ex=1)==1: mc.deleteUI(' ...
- 解析观察者模式在安卓程序中的应用——如何实现跨界面Handler通讯
这里特使用了github中的一个项目作为例子进行解析,地址为:https://github.com/LiqiNew/HandlerFrame/tree/master/handlerFrame/src/ ...
- 第七节《Git协议与工作协同》
Git提供了丰富的协议支持,包括:SSH.GIT.HTTPS.FTP.FTPS.RSYNC,这些协议可以分为两类:智能协议和哑协议. <1>智能协议 在会话时使用智能协议,会在会话的两个版 ...
- 分布式事务(二)Java事务API(JTA)规范
一.引子 既然出现了分布式场景(DTP模型), 大java也及时制定出一套规范来给各大应用服务器.数据库/mq等厂商使用,以方便管理互通--->JTA闪亮登场.JTA(Java Transact ...
- repo/repo init-解决同步源码Cannot get http://gerrit.googlesource.com/git-repo/clone.bundle
以下转自:http://www.cnblogs.com/dinphy/p/5669384.html 问题: fatal: Cannot get https://gerrit.googlesource. ...
- 解决STM32 I2C接口死锁在BUSY状态的方法讨论
关于STM32的I2C接口死锁在BUSY状态无法恢复的现象,网上已有很多讨论,看早几年比较老的贴子,有人提到复位MCU也无法恢复.只有断电才行的状况,那可是相当严重的问题.类似复位也无法恢复的情况是存 ...
- Proxifier代理工具简介和下载
Proxifier 是一款功能非常强大的socks5客户端,可以让不支持通过代理服务器工作的网络程序能通过HTTPS或SOCKS代理或代理链.支持64位系统,支持Xp,Vista,Win7,支持soc ...
- 新ubuntu系统装软件
新装的ubuntu系统安装软件: 1.ifconfig #sudo apt-get install net-tools 2.vim #sudo apt-get install vim 3.telnet ...
- python爬虫学习笔记(一)——环境配置(windows系统)
在进行python爬虫学习前,需要进行如下准备工作: python3+pip官方配置 1.Anaconda(推荐,包括python和相关库) [推荐地址:清华镜像] https://mirrors ...
- 一些常用的js循环,如for
https://blog.csdn.net/u014399368/article/details/82862444