[英]NotOLE2FileException while reading xls file in apache poi 4.0.1
I am trying to read xls
file in java
using apache poi 4.0.1
我正在尝试使用apache poi 4.0.1
在java
读取xls
文件
Problem 问题
When i try to read the file, i fet following exception 当我尝试读取文件时,出现以下异常
org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature;
Complete stacktrace 完整的堆栈跟踪
org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x756F4E2C65707954, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:151)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:117)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:294)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:401)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:382)
at com.wild.animal.identifier.DataLoader.loadData(DataLoader.java:27)
at com.wild.animal.identifier.WildAnimalSearch.main(WildAnimalSearch.java:6)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
When i try to open the same file manually in excel i get the following dialog 当我尝试在excel中手动打开同一文件时,出现以下对话框
Question 题
What am i doing wrong and how can i fix it? 我在做什么错,我该如何解决?
Code 码
public static void loadData() {
try {
File dataFile = new File("Attachment_1551892122.xls");
FileInputStream fins = new FileInputStream(dataFile);
HSSFWorkbook workbook = new HSSFWorkbook(fins);
HSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
Iterator<Cell> columnIterator;
Row row;
Cell column;
while (rowIterator.hasNext()) {
row = rowIterator.next();
columnIterator = row.cellIterator();
while (columnIterator.hasNext()) {
column = columnIterator.next();
switch (column.getCellType()) {
case STRING:
System.out.print(column.getStringCellValue());
break;
case BOOLEAN:
System.out.print(column.getBooleanCellValue());
break;
case NUMERIC:
System.out.print(column.getNumericCellValue());
break;
}
System.out.print(" - ");
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
As pointed out in a comment by @AxcelRichter, the file was not a excel file, so i created a new excel file, copied all the data from old file in to new one. 正如@AxcelRichter在评论中指出的那样,该文件不是excel文件,因此我创建了一个新的excel文件,将所有数据从旧文件复制到新文件中。
Tried reading the new file and it worked!! 尝试读取新文件,它起作用了!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.