关于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 ...
随机推荐
- 2017第八届蓝桥杯C/C++语言A组
一:题目: 标题:迷宫 X星球的一处迷宫游乐场建在某个小山坡上.它是由10x10相互连通的小房间组成的. 房间的地板上写着一个很大的字母.我们假设玩家是面朝上坡的方向站立,则:L表示走到左边的房间,R ...
- cordova插件新的窗口实例打开连接: cordova-plugin-inappbrowser
1. 添加插件:cordova plugin add cordova-plugin-inappbrowser : 2. InAppBrowser可以使用新的窗口实例打开连接,提供了地址栏的显示隐藏, ...
- MQTT研究之EMQ:【SSL双向验证】
EMQ是当前MQTT中,用于物联网领域中比较出色的一个broker,今天我这里要记录和分享的是关于SSL安全通信的配置和注意细节. 环境: 1. 单台Linux CentOS7.2系统,安装一个EMQ ...
- NanoPC-T4/RK3399开发板Ubuntu FriendlyCore系统开机自动运行客户程序
RK3399开机自动运行客户程序 比如hellohello.c 交叉编译:aarch64-linux-gcc hello.c -o hello使用SecureCRT软件通过串口下载到开发板rz修改文件 ...
- tensorflow训练中出现问题Couldn't open CUDA library cupti64_80.dll
参考链接:http://blog.csdn.net/lanchunhui/article/details/62242568 在代码中添加了tensorboard可视化代码后,原程序运行报错,以上链接方 ...
- 【Dubbo源码学习】负载均衡算法(2)-轮询算法的实现
@Overrideprotected <T> Invoker<T> doSelect(List<Invoker<T>> invokers, URL ur ...
- C# .net mvc web api 返回 json 内容,过滤值为null的属性
在WebApiConfig.Register 中增加一段 #region 过滤值为null的属性 //json 序列化设置 GlobalConfiguration.Configuration.Form ...
- SpringCloudConfig配置中心git库以及refresh刷新
基于git库的Spring Cloud Config配置中心代码demo下载地址:https://gitlab.com/mySpringCloud/config-git SpringBoot版本: 1 ...
- C++ 64位操作系统调用 RegOpenKey() 读取注册表,返回 2, ERROR_FILE_NOT_FOUND
环境:64位操作系统, VS2017 首先在命令行执行 REG ADD HKLM\Software\seastarsun /v serial /t REG_SZ /d 58ae4cb077a4e1 在 ...
- PXC5.7(Percona XtraDB Cluster)+HAproxy+Keepalived 集群部署
Percona-XtraDB-Cluster+Haproxy 搭建集群环境 环境准备及服务器信息: 配置防火墙 firewall-cmd --add-port=3306/tcp --permanent ...