1. 程式人生 > >POI抓取PPT圖表頁圖表標題

POI抓取PPT圖表頁圖表標題

應需求需要抓取PPT的圖表標題,poi可以實現這個功能,PDFbox和itext也可以,但是後兩者比較麻煩。

一、依賴

<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-scratchpad</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>ooxml-schemas</artifactId>
			<version>1.3</version>
		</dependency>

二、程式碼

public static void main(String[] args) {
        try {
            XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("C:\\Users\\Administrator\\Desktop\\a.pptx"));
            //迴圈幻燈片
            for (XSLFSlide S:ppt.getSlides()){
                for (POIXMLDocumentPart part :S.getRelations()){
                    //判斷是否有chart
                    if(part instanceof XSLFChart){
                        XSLFChart chart = (XSLFChart) part;
                        CTChart ctChart = chart.getCTChart();
                        CTTitle title = ctChart.getTitle();
                        if(title == null)
                            continue;
                        CTTx titleTx = title.getTx();
                        CTTextBody body = titleTx.getRich();
                        CTTextParagraph[] paragraphs = body.getPArray();
                        for(int i=0; i<paragraphs.length; i++) {
                            CTTextParagraph paragraph = paragraphs[i];
                            CTRegularTextRun[] runs = paragraph.getRArray();

                            String chartTitle = "";
                            for(int j=0; j<runs.length; j++) {
                                CTRegularTextRun run = runs[j];
                                String t = run.getT();
                                chartTitle += t;
                            }
                            System.out.println(chartTitle);
                        }

                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }