使用Apache POI确定MS Excel文件类型 [英] Determine MS Excel file type with Apache POI
问题描述
有没有办法确定Apache POI中的MS Office Excel文件类型?我需要知道什么格式的Excel文件:在Excel '97(-2007)(.xls)或Excel 2007 OOXML(.xlsx)。
Is there a way to determine MS Office Excel file type in Apache POI? I need to know in what format is the Excel file: in Excel '97(-2007) (.xls) or Excel 2007 OOXML (.xlsx).
我想我可以这样做:
int type = PoiTypeHelper.getType(file);
switch (type) {
case PoiType.EXCEL_1997_2007:
...
break;
case PoiType.EXCEL_2007:
...
break;
default:
...
}
谢谢。 / p>
Thanks.
推荐答案
对答案进行评论...
Promoting a comment to an answer...
如果你'要做一些特殊的文件,然后 rjokelai的答案是这样做的方式。
If you're going to be doing something special with the files, then rjokelai's answer is the way to do it.
但是,如果您只是要使用HSSF / XSSF / Common SS用户名,那么让POI为您做的更简单,并使用 WorkbookFactory 为您检测并打开类型。你可以这样做:
However, if you're just going to be using the HSSF / XSSF / Common SS usermodel, then it's much simpler to have POI do it for you, and use WorkbookFactory to have the type detected and opened for you. You'd do something like:
Workbook wb = WorkbookFactory.create(new File("something.xls"));
或
Workbook wb = WorkbookFactory.create(request.getInputStream());
然后如果你需要做一些特别的事情,测试它是否是一个 HSSFWorkbook
或 XSSFWorkbook
。打开文件时,如果可能,使用文件而不是InputStream 加快速度,节省内存。
Then if you needed to do something special, test if it's a HSSFWorkbook
or XSSFWorkbook
. When opening the file, use a File rather than an InputStream if possible to speed things up and save memory.
如果您不知道自己的文件是什么,请使用 Apache Tika 进行检测 - 它可以为您检测大量不同的文件格式。
If you don't know what your file is at all, use Apache Tika to do the detection - it can detect a huge number of different file formats for you.
这篇关于使用Apache POI确定MS Excel文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!