如何使用Apache POI从Powerpoint中的Slide Diagram替换DataXML

时间:2022-07-24 13:00:11

i want to replace the one data.xml file of power point presentation in java using apache API with other file data.xml

我想使用apache API和其他文件data.xml替换java中powerpoint表示的一个data.xml文件

For the reference i want to replace the following file with another power point file.

对于参考,我想用另一个电源点文件替换以下文件。

如何使用Apache POI从Powerpoint中的Slide Diagram替换DataXML

Following is the code i have tried but xml isnt replacing. I have different XML for both files every time i run after replacing using this code

以下是我尝试的代码,但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;
}

1 个解决方案

#1


0  

I added the few line after changing in order to save the file. The XMLSlideShow must write to some file after changing or adding.

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

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

#1


0  

I added the few line after changing in order to save the file. The XMLSlideShow must write to some file after changing or adding.

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

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