I am trying to read xls
file in java
using apache poi 4.0.1
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
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.
Tried reading the new file and it worked!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.