Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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();
                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 7, 2019 at 19:32
                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 8, 2019 at 9:16

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.