Apache POI 3.17读取PowerPoint文件PPT

2018-03-13 12:20:03
1394次阅读
0个评论

网上搜索到的POI读取ppt方法在最新jar包上大都已过时,这里用当前最新的jar包方法读取ppt

POI 操作office需要的jar包:

commons-collections4-4.1.jar
poi-3.17.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.17.jar
poi-scratchpad-3.17.jar
xmlbeans-2.6.0.jar

获取文件后缀名需引入

commons-io-2.4.jar


/**
	 * 读取PowerPoint文件
	 * @param filePath 文档路径
	 * @return
	 */
	public static String readPowerPoint(String filePath){
		File file = new File(filePath);
		//获得文件名  
        String fileName = org.apache.commons.io.FilenameUtils.getExtension(filePath);  
		//获取excel文件的io流  
    	InputStream is = null;
    	
    	HSLFSlideShow hslfSlideShow = null;
    	XSLFPowerPointExtractor xslfPowerPointExtractor = null;
    	
    	StringBuffer content = new StringBuffer("");
		try {
            if(fileName.equalsIgnoreCase("ppt")){  
            	is = new FileInputStream(file);
            	hslfSlideShow = new HSLFSlideShow(is);
            	List<HSLFSlide> slides = hslfSlideShow.getSlides();
                for(int i=0;i<slides.size();i++){
                	HSLFSlide xslfSlide = slides.get(i);
                	
                    //读取一张幻灯片的标题
                  //  String title=xslfSlide.getTitle();
                 //   System.out.println("标题:"+title);

                	//读取幻灯片的标题和内容
                    for (List<HSLFTextParagraph> txt : xslfSlide.getTextParagraphs()) {
                        for (HSLFTextParagraph para : txt) {
                            for (HSLFTextRun run : para) {
                            	String text = run.getRawText();
                            	content.append(text);
                            }
                        }
                    }
                }    
            }else if(fileName.equalsIgnoreCase("pptx")){  
            	OPCPackage opcPackage = POIXMLDocument.openPackage(filePath);  
            	
            	
				xslfPowerPointExtractor = new XSLFPowerPointExtractor(opcPackage);
				
				return xslfPowerPointExtractor.getText();
				  
				
            } 
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XmlException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (OpenXML4JException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if(hslfSlideShow != null){
				try {
					hslfSlideShow.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(xslfPowerPointExtractor != null){
				try {
					xslfPowerPointExtractor.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}  
		return content.toString();
	}




测试一下


public static void main(String[] args) {
	System.out.println(POI.readPowerPoint("c://1.ppt"));
	System.out.println(POI.readPowerPoint("c://1.pptx"));
	
}
收藏00

登录 后评论。没有帐号? 注册 一个。