在Java程式設計中,如何合併兩個PPT文件?
注意:需要存取網址:http://poi.apache.org/download.html , 下載一個Apache POI軟體包。這裡下載最新版本:poi-bin-3.17-20170915.tar.gz解壓並全部匯入 。
需要匯入全部包,如下圖所示 -
以下是使用Java合併兩個PPT文件的程式。
package com.yiibai;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class MergingMultiplePresentations {
public static void main(String args[]) throws IOException {
// creating empty presentation
XMLSlideShow ppt = new XMLSlideShow();
// taking the two presentations that are to be merged
String file1 = "aboutus1.pptx";
String file2 = "aboutus2.pptx";
String[] inputs = { file1, file2 };
for (String arg : inputs) {
FileInputStream inputstream = new FileInputStream(arg);
XMLSlideShow src = new XMLSlideShow(inputstream);
for (XSLFSlide srcSlide : src.getSlides()) {
// merging the contents
ppt.createSlide().importContent(srcSlide);
}
}
String file3 = "aboutus12.pptx";
// creating the file object
FileOutputStream out = new FileOutputStream(file3);
// saving the changes to a file
ppt.write(out);
System.out.println("Merging done successfully");
out.close();
}
}
執行上面範例程式碼,得到以下結果 -
Merging done successfully
這就建立一個PPT檔案:aboutus12.pptx了,如下所示 -