我使用的Apache POI 3.16這裏閱讀Excel文件是我的代碼爲什麼我得到這個異常:異常在線程「主要」 org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException
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)
請幫助我,謝謝。
'NotOfficeXmlFileException' - 這是在等一個XLSX,而不是一個XLS。 –
我對Excel文件的閱讀並不完全熟悉,但是您是否嘗試將其轉換爲更新的.xlsx格式?這只是一種預感,所以我可能是錯的。 –
但我有xls,我不想用xlsx重新保存它,我該如何實現? –