如何修改ppt内置的excel中的数据

问题描述 投票:0回答:1

enter image description here 我的ppt模板中有一个内置的excel。现在想用poi修改excel中的数据。我已经尝试了很多方法,但仍然不起作用。请帮助我。

 public static void replaceExcel(XMLSlideShow ppt, Map<String, Object> excelDataMap) throws IOException {

        List<XSLFSlide> slides = ppt.getSlides();
        for (XSLFSlide slide : slides) {
            List<XSLFShape> shapes = slide.getShapes();
            for (int i = 0; i < shapes.size(); i++) {
                XSLFShape shape = slide.getShapes().get(i);
                if (shape instanceof XSLFObjectShape) {
                    PackagePart packagePart = slide.getPackagePart();
                    System.out.println(packagePart.getContentType());
                    System.out.println(shape.getShapeName());
                    XSLFObjectShape excelShape = (XSLFObjectShape) shape;
                    String shapeName = excelShape.getShapeName();
                    InputStream inputStream = excelShape.getObjectData().getInputStream();;
                    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);




                }
            }

        }
    }

异常

java.lang.ClassCastException: class org.apache.poi.ooxml.POIXMLDocumentPart cannot be cast to class org.apache.poi.xslf.usermodel.XSLFObjectData (org.apache.poi.ooxml.POIXMLDocumentPart and org.apache.poi.xslf.usermodel.XSLFObjectData are in unnamed module of loader 'app')

    at org.apache.poi.xslf.usermodel.XSLFObjectShape.getObjectData(XSLFObjectShape.java:95)
    at utils.ppt.PPTUtils.replaceExcel(PPTUtils.java:152)
    at TestDemo.testPPT(TestDemo.java:475)

我希望的效果是把ppt中的数据写入excel

java excel apache-poi powerpoint
1个回答
0
投票

我将其称为 XSLFObjectShape.getObjectData

中的错误
@Override
public XSLFObjectData getObjectData() {
    String oleRel = getCTOleObject().getId();
    return getSheet().getRelationPartById(oleRel).getDocumentPart();
}

对我来说这味道很难闻。

这里

getSheet().getRelationPartById(oleRel).getDocumentPart()
返回
org.apache.poi.ooxml.POIXMLDocumentPart
。那么为什么这甚至可以编译呢?
XSLFObjectData
延伸
org.apache.poi.ooxml.POIXMLDocumentPart
。所以
getSheet().getRelationPartById(oleRel).getDocumentPart()
may
XSLFObjectData
,因此隐式向下转型是 possible。但它不需要是这样的。因此,铸造也可能会失败,就像你一样。

更好的是:

@Override
public XSLFObjectData getObjectData() {
    String oleRel = getCTOleObject().getId();
    org.apache.poi.ooxml.POIXMLDocumentPart documentPart = getSheet().getRelationPartById(oleRel).getDocumentPart();
    XSLFObjectData objectData = new XSLFObjectData(documentPart.getPackagePart());
    return objectData;
}

应该有效的完整示例:

import java.io.InputStream;
import java.io.FileInputStream;

import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class PPTXGetXSLFObjectShapes {
    
 static XSLFObjectData getObjectData(XSLFObjectShape objectShape) {
  String oleRel = objectShape.getCTOleObject().getId();
  org.apache.poi.ooxml.POIXMLDocumentPart documentPart = objectShape.getSheet().getRelationPartById(oleRel).getDocumentPart();
  XSLFObjectData objectData = new XSLFObjectData(documentPart.getPackagePart());
  return objectData;
 }
              
 public static void main(String[] args) throws Exception {
     
  XMLSlideShow slideShow = new XMLSlideShow(new FileInputStream("./PPTXPresentation.pptx"));
  
  for (XSLFSlide slide : slideShow.getSlides()) {
   //System.out.println(slide);        
   for (XSLFShape shape : slide.getShapes()) {
    //System.out.println(shape); 
    
    if (shape instanceof XSLFObjectShape) {
     XSLFObjectShape objectShape = (XSLFObjectShape)shape;
     System.out.println(objectShape); 
     //XSLFObjectData objectData = objectShape.getObjectData(); //fails when XSLFObjectData is embedded file
     XSLFObjectData objectData = getObjectData(objectShape);
     System.out.println(objectData);
     if ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".equals(objectData.getPackagePart().getContentType())) {     
      InputStream inputStream = objectData.getInputStream();
      XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
      System.out.println("Workbook created: " + workbook);
     }   
    }
    
   }       
  }
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.