java 如何使用 POI 处理旧的 excel .xls 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15336457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to process old excel .xls files using POI?
提问by Wael
I switched from jxl to poi since POI has more features. However, I wasn't able to process the xls files that were generated in the old format. Now I am getting this error:
我从 jxl 切换到 poi,因为 POI 有更多功能。但是,我无法处理以旧格式生成的 xls 文件。现在我收到此错误:
org.apache.poi.hssf.OldExcelFormatException: The supplied spreadsheet seems to be Excel 5.0/7.0 (BIFF5) format. POI only supports BIFF8 format (from Excel versions 97/2000/XP/2003)
org.apache.poi.hssf.OldExcelFormatException:提供的电子表格似乎是 Excel 5.0/7.0 (BIFF5) 格式。POI 仅支持 BIFF8 格式(来自 Excel 版本 97/2000/XP/2003)
Now I am thinking to use both JXL as wells as POI depending on the xls version so for old format xls files I will use jxl while for newer versions I will use POI. Is this a good solution? Are there any alternatives?
现在我想根据 xls 版本同时使用 JXL 和 POI,因此对于旧格式的 xls 文件,我将使用 jxl,而对于较新的版本,我将使用 POI。这是一个很好的解决方案吗?有没有其他选择?
采纳答案by dan
For old Excel format files, you have the following alternatives:
对于旧的 Excel 格式文件,您有以下选择:
- HSSF, the
POI
implementation of the Excel '97(-2007) file format.- If you just want to extract the textual content, then you can use OldExcelExtractorwhich will pull only the text and numbers from the file.
- If you need the values from a specific cells, then you'll need to take an
approach a bit like
OldExcelExtractor
, process the file at the record level, and check for the co-ordinates on OldStringRecord, NumberRecord, OldFormulaRecordand friends.
- Like you already mentioned, JXLcan handle some cases too.
- Use a JDBC/ODBCdriver. It is not as flexible as
HSSF
but for some old formats it is the only way to extract the information.
- HSSF,
POI
Excel '97(-2007) 文件格式的实现。- 如果您只想提取文本内容,则可以使用OldExcelExtractor,它只会从文件中提取文本和数字。
- 如果您需要来自特定单元格的值,那么您需要采用类似于 的
OldExcelExtractor
方法,在记录级别处理文件,并检查OldStringRecord、NumberRecord、OldFormulaRecord和朋友的坐标。
- 正如您已经提到的,JXL也可以处理某些情况。
- 使用JDBC/ODBC驱动程序。它不像
HSSF
某些旧格式那样灵活,但它是提取信息的唯一方法。
回答by Anish6595
as per my knowledge you can use this code to read excel files of the .xls format
据我所知,您可以使用此代码读取 .xls 格式的 excel 文件
FileInputStream in=new FileInputStream(new File("filename.xls"));
Wookbook wb=new HSSFWorkbook(in);
to read the new excel versions(2007 and up):
阅读新的 excel 版本(2007 及更高版本):
FileInputStream in=new FileInputStream(new File("filename.xls"));
Wookbook wb=new XSSFWorkbook(in);
external jar files that you will need:
您将需要的外部 jar 文件:
1. poi-3.9
2. dom4j-1.6.1
3. XMLbeams-2.5.0
if you're work only requires you to work with .xls then only poi-3.0 will suffice. You need the other jars to work witht the new versions of excel.
如果您的工作只需要使用 .xls,那么只有 poi-3.0 就足够了。您需要其他 jars 才能使用新版本的 excel。