如何使用 Apache POI 替换 Powerpoint 幻灯片图中的 DataXML

How to replace DataXML from Slide Diagram in Powerpoint using Apache POI

我想用其他文件 data.xml

替换 java 使用 apache API 的 powerpoint 演示文稿的一个 data.xml 文件

为了参考,我想用另一个 powerpoint 文件替换以下文件。

以下是我试过的代码,但 xml 没有替换 。每次我 运行 使用此代码

替换后,我对两个文件都有不同的 XML
public static void main(String[] args) {
    // TODO Auto-generated method stub
    final String filename = "C:/Users/skhan/Desktop/game.pptx";
    final String filename1 = "C:/Users/skhan/Desktop/globe.pptx";

    try {


        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filename));
        OPCPackage pkg = ppt.getPackage();
        PackagePart data = pkg.getPart(
               PackagingURIHelper.createPartName("/ppt/diagrams/data1.xml"));
        InputStream data1Inp = data.getInputStream();

        XMLSlideShow ppt1 = new XMLSlideShow(new FileInputStream(filename1));
        OPCPackage pkg1 = ppt1.getPackage();
        PackagePart data11 = pkg1.getPart(
               PackagingURIHelper.createPartName("/ppt/diagrams/data1.xml"));
        InputStream data1Inp1 = data11.getInputStream();

        String data1String = GetData(data1Inp);
        String data2String = GetData(data1Inp1);



//i want to replace here
        PrintStream pr = new PrintStream(data.getOutputStream());
        pr.print(data2String);
        pr.close();


        System.out.println("Completed");


    } catch (Exception e) {

        e.printStackTrace();
    }

}


public static String GetData(InputStream input) throws Exception
{
    StringBuilder builder = new StringBuilder();
    int ch;
    while((ch = input.read()) != -1){
        builder.append((char)ch);
    }

    String theString = builder.toString();
    return theString;
}

为了保存文件,我在更改后添加了几行。 XMLSlideShow 必须在更改或添加后写入某个文件。

        File file =new File(filename);
        FileOutputStream out = new FileOutputStream(file);
        ppt.write(out);
        out.close();