简体   繁体   中英

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

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

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

在此处输入图片说明

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM