0

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

enter image description here

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();
    }
}
  • 1
    `Invalid header signature; read 0x756F4E2C65707954`: Your `Attachment_1551892122.xls` file is not a `Excel` file but a plain text file. It starts with the text "Type,Nou". Where is the file from? – Axel Richter Mar 07 '19 at 19:32
  • @AxelRichter its from a client for whom i am developing an app –  Mar 07 '19 at 19:42
  • Seems to be a `CSV` file and "Type,Nou" is part of the first line with the field names. But `CSV` files should not be named `*.xls` but `*.csv`. `Excel` can opening `CSV` files but `apache poi` is not able parsing `CSV` files. – Axel Richter Mar 08 '19 at 09:16

1 Answers1

0

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!!