最近使用POI 抽取office三件套里面的数据。主要就是word、ppt、excel中的内容。
一、数据准备:
(一)、版本:
POI版本:4.1.2
office:97-2010
(二)、使用组件:
主要使用了POI自带的extractor。根据office版本不同,使用的组件也不同:
使用的具体extractor:
1、基于OOXML(office2007以后)
XSSFExcelExtractor
SlideShowExtractor(代替了XSLFPowerPointExtractor,4.0.0版本后作废)
XWPFWordExtractor
2、基于OLE2(office2007以前)
ExcelExtractor
WordExtractor
Word6Extractor(对于更老的 (Word 6 / Word 95)支持)
SlideShowExtractor(代替了PowerPointExtractor,4.0.0版本后作废)
二、对于性能
POI由于内容占用的问题,不推荐使用流加载方式(FileInputStream),或者可以使用alibaba的easyexcel(这里不扯了)
默认使用:
XML:OPCPackage
OLE2:POIFSFileSystem(4.0.0版本后不区分POIFSFileSystem和NPOIFSFileSystem了)打开。
三、事例用法:
public void testExcelExtractor() throws IOException {
File doc = new File(excel_file);
POIFSFileSystem fs = new POIFSFileSystem(doc);
ExcelExtractor ex=new ExcelExtractor(fs);
System.out.println(ex.getText());
ex.close();
fs.close();
}
public void testExcel2007Extractor() throws IOException, XmlException, OpenXML4JException {
File doc = new File(xlsx_file);
OPCPackage pkg = OPCPackage.open(doc);
XSSFExcelExtractor ex = new XSSFExcelExtractor(pkg);
System.out.println(ex.getText());
ex.close();
// pkg.close();
}
public void testWordExtractor() throws IOException {
File doc = new File(word_file);
POIFSFileSystem fs = new POIFSFileSystem(doc);
WordExtractor ex = new WordExtractor(fs);
System.out.println(ex.getText());
ex.close();
fs.close();
}
public void testWord2007Extractor() throws IOException, XmlException, OpenXML4JException {
File doc = new File(docx_file);
OPCPackage pkg = OPCPackage.open(doc);
XWPFWordExtractor ex = new XWPFWordExtractor(pkg);
System.out.println(ex.getText());
ex.close();
// pkg.close();
}
public void testPPTExtractor() throws IOException {
File doc = new File(ppt_file);
POIFSFileSystem fs = new POIFSFileSystem(doc);
PowerPointExtractor ex = new PowerPointExtractor(fs);
System.out.println(ex.getText());
ex.close();
fs.close();
}
public void testPPT2007Extractor() throws IOException, XmlException, OpenXML4JException {
File doc = new File(pptx_file);
OPCPackage pkg = OPCPackage.open(doc);
XSLFPowerPointExtractor ex = new XSLFPowerPointExtractor(pkg);
System.out.println(ex.getText());
// ex.close();
pkg.close();
}
public void testPPTOtherExtractor() throws IOException, XmlException, OpenXML4JException {
File doc = new File(ppt_file);
POIFSFileSystem fs = new POIFSFileSystem(doc);
SlideShow<HSLFShape, HSLFTextParagraph> sss = new HSLFSlideShow(fs);
SlideShowExtractor<HSLFShape, HSLFTextParagraph> ex = new SlideShowExtractor<HSLFShape, HSLFTextParagraph>(sss);
System.out.println(ex.getText());
ex.close();
fs.close();
}
public void testPPT2007OtherExtractor() throws IOException, XmlException, OpenXML4JException {
File doc = new File(pptx_file);
OPCPackage pkg = OPCPackage.open(doc);
SlideShow<XSLFShape, XSLFTextParagraph> sss = new XMLSlideShow(pkg);
SlideShowExtractor<XSLFShape, XSLFTextParagraph> ex = new SlideShowExtractor<XSLFShape, XSLFTextParagraph>(sss);
System.out.println(ex.getText());
ex.close();
// pkg.close();
}
其中,ppt比较特殊,原来的方法全部作废,而统一使用SlideShowExtractor。
例子里也写了原始的方法,不过都Deprecated了