我想用java解析Excel文件,所以我使用apache poi库,这里是maven依赖项:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
这将包括一系列依赖项:
poi-ooxml-3.14.jar
poi-3.14.jar
commons-codec-1.10.jar
poi-ooxml-schemas-3.14.jar
xmlbeans-2.6.0.jar
stax-api-1.0.1.jar
curvesapi-1.03.jar
当我尝试使用以下代码读取 Office 365 Excel 文件 (.xlsx) 时:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelConverter {
public static void main(String[] args) throws Exception{
String excelFilePath = "C:/temp/Book1.xlsx";
File myFile = new File(excelFilePath);
System.out.println("File exists: " + myFile.exists());
FileInputStream inputStream = new FileInputStream(myFile);
Workbook workbook = new XSSFWorkbook(inputStream);
}
}
我收到以下控制台消息:
File exists: true
Exception in thread "main" org.apache.poi.POIXMLException: Strict OOXML isn't currently supported, please see bug #57699
at org.apache.poi.POIXMLDocumentPart.getPartFromOPCPackage(POIXMLDocumentPart.java:679)
at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:122)
at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:115)
at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:61)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:273)
at org.myCompany.excel.ExcelConverter.main(ExcelConverter.java:25)
你知道我能做什么来解决这个问题吗?
提前致谢
目前,除了“不要以“严格 OOXML”格式保存电子表格”之外,似乎没有任何简单的解决方案。
例如,在 Excel 中使用
Save As --> "Excel Workbook (.xlsx)"
代替
Save As --> "Strict Open XML Spreadsheet (.xlsx)"
您知道为什么 Excel Worksheet 和这种格式具有相同的文件扩展名吗?
这个问题只有微软才能回答。但我猜工程师(或他们的管理层)并没有预料应用软件有必要做出区分。
我接受文件作为输入,然后根据扩展名处理它们。没有try-catch我怎么知道?
没有任何东西可以让您使用当前一代 POI 处理文档。
I guess you could code something to read the file and look for the signature for "strict OOXML" format1 before passing the file to POI, but there's not much point. You would be writing a stack of extra code just so that you can replace the try-catch with other logic.
1 - See https://www.loc.gov/preservation/digital/formats/fdd/fdd000395.shtml#sign https://www.loc.gov/preservation/digital/formats/fdd/fdd000395.shtml#sign
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)