是否有统一的 ExcelExtractor class 和 xls 和 xlsx 文件的工厂?

Is there a uniform ExcelExtractor class and a factory for both xls and xlsx files?

是否有通用的 class 和 ExcelExtractor 接口的实现来统一处理从 xls 和 xlsx 源中提取文本? 也许,ss 包中的东西。

我正在寻找能让我做类似事情的东西,但要根据文件类型从工厂获得正确的实施。

现在,我必须明确地使用 org.apache.poi.hssf.extractor.ExcelExtractor 对于 xls 文件和 org.apache.poi.xssf.extractor.XSSFExcelExtractor 对于 xlsx。

例如,xls 的显式方法:

InputStream inp = new FileInputStream(path);
HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
ExcelExtractor extractor = new ExcelExtractor(wb);

extractor.setFormulasNotResults(true);
extractor.setIncludeSheetNames(false);
String text = extractor.getText();

我可以实现自己的工厂,但在我这样做之前,我想问问是否有处理这两种格式的通用方法(这就是 ss 包的用途)。

两个选项

首先,如果您真的想坚持使用旧的 Apache POI 文本提取器,请使用 ExtractorFactory class。这将识别类型,并为您创建一个提取器

但是,更好的选择 - Apache Tika. Tika builds on top of POI (and lots of others), and gives you plain text extraction (+detection +xhtml +more!) from a wide range of file formats. You'd just call Tika, ask for the text, and get it back no matter the type. See Tika examples like this one 开始