1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java poi 生成ppt表格 关于java使用POI导出ppt 其中表格setText 失败问题

java poi 生成ppt表格 关于java使用POI导出ppt 其中表格setText 失败问题

时间:2022-08-22 13:29:52

相关推荐

java poi 生成ppt表格 关于java使用POI导出ppt  其中表格setText 失败问题

1、导出ppt 必要的包

使用maven

org.apache.poi

poi-ooxml

3.9

org.apache.poi

poi-ooxml-schemas

org.apache.poi

ooxml-schemas

1.1

2、poi-ooxml-schemas 和ooxml-schemas 的区别

3、应用

3.1、表格操作

public voidparsing(XSLFSlide slide, XSLFTable table) {//for循环增加数据,忽略标题行

List tableRow =table.getRows();

List cells = tableRow.get(1).getCells();while(table.getRows().size()

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()

table.getRows().get(i+ 1).addCell();

}

table.getRows().get(i+ 1).getCells().get(i1).setText(key);

}

}

}

3.2、替换图片操作

插入图片自己查资料,这里不提

这里只说读取模板ppt,并替换特定区域成图片。

if(shape[i] instanceofXSLFAutoShape){

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 voidsetTextBox(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、文档资料

在子项目 pptXSLF 中。

6、个人说明

如果你们发现其他问题或者解决方法,可以在评论区里提出。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。