gpt4 book ai didi

java - 为什么会出现此异常 : Exception in thread "main" org. apache.poi.openxml4j.exceptions.NotOfficeXmlFileException

转载 作者:行者123 更新时间:2023-11-30 06:37:34 26 4
gpt4 key购买 nike

我正在使用 apache poi 3.16 读取 excel 文件,这是我的代码

try
{
String excelPath = "C:\\Users\\wecme\\Desktop\\AccountStatement.xls";
FileInputStream fileInputStream = new FileInputStream(new File(excelPath));

// Create Workbook instance holding .xls file
XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

// Get the first worksheet
XSSFSheet sheet = workbook.getSheetAt(0);

// Iterate through each rows
java.util.Iterator<Row> rowIterator = sheet.iterator();

while (rowIterator.hasNext())
{
// Get Each Row
Row row = rowIterator.next();

// Iterating through Each column of Each Row
java.util.Iterator<Cell> cellIterator = row.cellIterator();

while (cellIterator.hasNext())
{

Cell cell = cellIterator.next();

// Checking the cell format
switch (cell.getCellType())
{
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue() + "\t");
break;
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue() + "\t");
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue() + "\t");
break;


}
}
System.out.println("");
}

} catch (IOException ie)
{
ie.printStackTrace();
}

我的 Excel 数据如下所示:

Datetime    Description TransactionId   Credit Amount   Debit Amount    Remaining   OdAmount    EnteredBy   Remarks
1/6/2017 8:14 IDEA (9542010237) COMMISSION GLGHQN 0.31 0 2721.92 0 iNHYD0390437LO IDEA COMMISSION

当我读取其他没有时间正确读取日期的文件时,但是当我尝试读取此文件时,会出现此异常

Exception in thread "main" org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file
at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:286)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:758)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:37)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:291)
at excelRead.ReadExcel.main(ReadExcel.java:27)

请帮帮我,谢谢。

最佳答案

您正在创建一个 XSSFWorkbook,它仅适合处理 xlsx 文件,即新的基于 xml 的 Office 格式。然而,您的文件扩展名 (xls) 表明这是较旧的 Microsoft Office(我猜是 2003)格式。为了修复该错误,您需要将文件转换为 xlsx 格式,或者需要使用 HSSFWorkbook

您可以在这里找到一些有关 HSSF 的示例:https://poi.apache.org/spreadsheet/examples.html#hssf-only有关 XDDF 的一些示例可以在这里找到:https://poi.apache.org/spreadsheet/examples.html#xssf-only

关于java - 为什么会出现此异常 : Exception in thread "main" org. apache.poi.openxml4j.exceptions.NotOfficeXmlFileException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44964363/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com