Java 有没有办法使用 Apache POI 读取 .xls 和 .xlsx 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19129961/
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
Is there any way to read both .xls and .xlsx files using Apache POI?
提问by TOMAS DEL CASTILLO
I need to create a method that can read both xls and xlsx files. According to my research, HSSF is used to read xls and XSSF to read xlsx. Is there a part of the Apache POI I can use to read both files? I also came across the ss.usermodel but found no sufficient codes that will entertain both xls and xlsx....
我需要创建一个可以读取 xls 和 xlsx 文件的方法。根据我的研究,HSSF用于读取xls和XSSF用于读取xlsx。我可以使用 Apache POI 的一部分来读取这两个文件吗?我也遇到了 ss.usermodel 但发现没有足够的代码可以同时满足 xls 和 xlsx ....
采纳答案by Sumit Gupta
I haven't had much exp with Apache POI, but as far as i know if you refer to a workbook by class "Workbook" then you can read and write both xls & xlsx.
我对 Apache POI 的经验不多,但据我所知,如果您通过“工作簿”类引用工作簿,那么您可以读写 xls 和 xlsx。
All you have to do is when creating object write
您所要做的就是在创建对象时写入
for .xls-
对于 .xls-
Workbook wb = new HSSFWorkbook();
for .xlsx-
对于 .xlsx-
Workbook wb = new XSSFWorkbook();
you can pass a parameter for file type and create the WorkBook object accordingly using If statement.
您可以传递文件类型的参数并使用 If 语句相应地创建 WorkBook 对象。
回答by Balaji Krishnan
one option would be to check the file name with lastIndexOf for . and see if it is .xls or xlsx and then use an if condition to switch accordingly. been a long time since i worked on poi but i think it the attributes are like HSSF for .xls and XSSF for .xlsx refer http://poi.apache.org/site, last line under the topic Why should I use Apache POI?
一种选择是使用 lastIndexOf 检查文件名。并查看它是 .xls 还是 xlsx,然后使用 if 条件进行相应的切换。自从我在 poi 上工作已经很长时间了,但我认为它的属性就像 .xls 的 HSSF 和 .xlsx 的 XSSF 参考http://poi.apache.org/站点,主题下的最后一行为什么我应该使用 Apache POI ?
回答by Anantha Sharma
It appears you are looking for a way to abstract the read process, you are saying it doesn't matter if its XLS or XLSX, you want your code to work without modification.
看来您正在寻找一种抽象读取过程的方法,您是说无论是 XLS 还是 XLSX,您都希望代码无需修改即可工作。
I'd recommend you to look at Apache Tika, its an awesome library that abstracts file reading and content parsing, it uses POI and many other libraries and has a nice abstraction to all of them.
我建议您查看Apache Tika,它是一个很棒的库,可以抽象文件读取和内容解析,它使用 POI 和许多其他库,并且对所有这些库都有很好的抽象。
reading a PDF/XLS/XLSX is similar to reading a text file, all the work is done behind the scene.
阅读 PDF/XLS/XLSX 类似于阅读文本文件,所有工作都在幕后完成。
read this for more. http://www.searchworkings.org/blog/-/blogs/introduction-to-apache-tika
阅读本文了解更多信息。http://www.searchworkings.org/blog/-/blogs/introduction-to-apache-tika
回答by tom
Yes, there's a new set of interfaces provided by POI that work with both types.
是的,POI 提供了一组适用于这两种类型的新接口。
Use the WorkbookFactory.create() method to get a Workbook: http://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html
使用 WorkbookFactory.create() 方法获取工作簿:http: //poi.apache.org/apidocs/org/apache/poi/ss/usermodel/WorkbookFactory.html
You can check for excel files without relying on file extensions (which are unreliable - many csv files have xls extensions for example but cannot be parsed by POI) using the following:
您可以在不依赖文件扩展名的情况下检查 excel 文件(这是不可靠的 - 例如,许多 csv 文件具有 xls 扩展名,但不能被 POI 解析)使用以下命令:
//simple way to check for both types of excel files
public boolean isExcel(InputStream i) throws IOException{
return (POIFSFileSystem.hasPOIFSHeader(i) || POIXMLDocument.hasOOXMLHeader(i));
}
回答by Amit
Thanks to Tom's answer just to add, use foll. code to get inputstream else we may face Exception in thread "main" java.io.IOException: mark/reset not supported
感谢汤姆的回答只是补充,使用 foll。获取输入流的代码,否则我们可能会遇到Exception in thread "main" java.io.IOException: mark/reset not supported
InputStream inputStream = new FileInputStream(new File("C:\myFile.xls"));
if(! inputStream.markSupported()) {
inputStream = new PushbackInputStream(fileStream, 8);
}
回答by Prashant Gautam
you can read using poi-ooxml and poi-ooxml-schema jars provided by apache.
您可以使用 apache 提供的 poi-ooxml 和 poi-ooxml-schema jars 进行阅读。
and use below code:--
并使用以下代码:--
Workbook wb = null;
excelFileToRead = new FileInputStream(fileName);
wb = WorkbookFactory.create(excelFileToRead);
Sheet sheet = wb.getSheet(sheetName);
the above code will read both xls and xlsx files
上面的代码将同时读取 xls 和 xlsx 文件
回答by tanle
You can use
您可以使用
Workbook wb = WorkBookFactory().create(inputStream);