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
相关话题
- springboot读取配置文件
- Apache commons compress文件打包、压缩
- Apache-Commons CSV读写文件
- poi导出excel
- SpringBoot自定义配置文件及读取配置文件
- Apache的commons-net实现FTP的文件上传下载
- java反射根据字段名读取值
- Apache Commons Compress zip压缩解压
- 解决:Plugin org.apache.maven.plugins:maven-resources-plugin:2.6错误
- Apache Commons JEXL实现字符串转换成可执行代码
- 文件流式上传
- SpringMVC流式上传文件
- Nginx支持字体文件
- Spring MVC 文件下载
- Javacsv读写csv文件