码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2019-01-25 17:43:14      阅读:319      评论:0      收藏:0      [点我收藏+]

标签:move   cells   inpu   方法   rem   3.1   public   code   print   

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);
            }
        }

    }

  git项目:https://code.aliyun.com/1003771635/document.git

  在子项目 pptXSLF 中。

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标题。

 

5、个人说明

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

 

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

标签:move   cells   inpu   方法   rem   3.1   public   code   print   

原文地址:https://www.cnblogs.com/alvis128/p/10320436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!