POI操作PPT文檔(導入,導出,讀取,添加,拼接,替換文本,頁面排序)
注意:直接讀取.pptx文件時不能使用${user}這樣的作爲替換標識,在讀取.pptx文件時會解析成三段文本"${","user"和“}”。此文檔方法一操作.pptx文件,使用{user}作爲替換標識;方法二操作ppt的.xml文件,使用${user}作爲替換標識。此文檔使用poi-3.17,使用其他版本方法會有差異。
需要訪問網址http://poi.apache.org/download.html,載Apache POI軟件包。這裏下載:poi-bin-3.17-20170915.tar.gz解壓並全部導入 。
方法一:將三個ppt拼接成一個並重新排序,更改其中的文本內容,不會保留其原來的樣式,需要在代碼裏設置。
public static void main(String[] args) throws IOException {
Calendar cale = Calendar.getInstance();
int year = cale.get(Calendar.YEAR);
int month = cale.get(Calendar.MONTH);//月份從0開始
//導入待拼接的.pptx文件
InputStream coveris = new FileInputStream(new File("C:\\Users\\12456\\Desktop\\cover.pptx"));
InputStream datais = new FileInputStream(new File("C:\\Users\\12456\\Desktop\\201807.pptx"));
XMLSlideShow coverppt = new XMLSlideShow(coveris);//待拼接PPT1
XMLSlideShow datappt = new XMLSlideShow(datais);//待拼接PPT2
XMLSlideShow headppt = getPPT(); //此方法返回待拼接PPT3
//將PPT3依次插入到PPT2的末尾,createSlide()是在末尾創建
for (XSLFSlide srcSlide : headppt.getSlides()) {
datappt.createSlide().importContent(srcSlide);
}
//下面代碼 是將當前月份的數值所對應ppt中的第幾張 插入到PPT2的末尾
List<XSLFSlide> coverlist=coverppt.getSlides();
for (int i = 0; i < coverlist.size(); i++) {
if (i==month) {
datappt.createSlide().importContent(coverlist.get(i));
}
}
//此時PPT1,PPT3都已插入到PPT2中,現在進行重新排序, setSlideOrder(第幾張PPT(slide), 要去的位置(int));
datappt.setSlideOrder(datappt.getSlides().get(7), 0);
datappt.setSlideOrder(datappt.getSlides().get(5), 1);
datappt.setSlideOrder(datappt.getSlides().get(6), 2);
//以下爲導出PPT的操作
String filePath = "C:\\Users\\12456\\Desktop\\pptresult";
filePath = filePath.replace("\\", "\\\\");
File dir = new File(filePath);
if(!dir.exists()){
dir.mkdirs();
}
String path = dir.getPath();
path = path.replace("\\", "\\\\");
FileOutputStream out = new FileOutputStream(path+"\\天氣會商製作.pptx");
datappt.write(out);
out.close();
}
public static XMLSlideShow getPPT() throws IOException {
//導入要修改的PPT
InputStream is = new FileInputStream(new File("C:\\Users\\12456\\Desktop\\服務簡報內容\\模板.pptx"));
XMLSlideShow ppt = new XMLSlideShow(is);
List<XSLFSlide> Slides = ppt.getSlides();
for (XSLFSlide slide : Slides) {
List<XSLFShape> shapes = slide.getShapes();
for (XSLFShape shape : shapes) {
if (shape != null) {
if (shape instanceof AutoShape) {
try {
if (((AutoShape) shape).getText().contains("{ye}")) {
// 替換文字內容.用TextRun獲取替換的文本來設置樣式
TextRun rt=((AutoShape) shape).setText(((AutoShape) shape).getText().replace("{ye}", "2018"));
rt.setFontColor(Color.red);
rt.setFontSize(13.5);
rt.setFontFamily("微軟雅黑");
((AutoShape) shape).setText(((AutoShape) shape).getText().replace("{mo}", "7"));
}
// System.out.println(((AutoShape)shape).getText());
} catch (Exception ex) {
ex.printStackTrace();
}
// }else {
// System.out.println("Process me: " +
// shape.getClass());
}
}
}
}
return ppt;
}
方法二:操作ppt的.xml文件,.xml文件中使用${user}作爲替換標識,保留原來的樣式
public static void main(String[] args) throws IOException {
Map<String, String> datamap=new HashMap<String,String>();
datamap.put("year", "2018-7");
datamap.put("company", "**有限公司");
datamap.put("plan", "123");
datamap.put("actr", "456");
datamap.put("bias", "78");
datamap.put("cha", "123");
datamap.put("jing", "456");
datamap.put("bao", "789");
datamap.put("month", "4");
datamap.put("pric", "789");
datamap.put("one", " 123456789");
datamap.put("two", " 876555378");
//設置模板格式和路徑
Configuration configuration =new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File("D:\\JavaWorkSource"));
//讀取模板路徑下的PPT的.xml模板文件
Template template=configuration.getTemplate("ppttemp.xml","utf-8");
//設置導出路徑
File outfile=new File("C:\\Users\\12456\\Desktop\\result.xml");
Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile)), 10240);
try {
//將數據替換並導出PPT
template.process(datamap, out);
} catch (TemplateException e) {
e.printStackTrace();
}finally {
out.close();
}
}