Home » excel » java – ApachePOI isn't recognizing Excel .csv file. Invalid Header Signature error

java – ApachePOI isn't recognizing Excel .csv file. Invalid Header Signature error

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m trying to parse an excel file with the ending .csv using Java. After doing some research, I downloaded and installed the ApachePOI library. However, every time I try to open the excel file to be parsed, I get the following error:

    Exception in thread "main" java.io.IOException: Invalid header signature; read 0x4E2C53454C494D53, expected 0xE11AB1A1E011CFD0
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:322)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:303)
at excellibrarycreation.ExcelFileProcesser.processFile(ExcelFileProcesser.java:40)
at excellibrarycreation.ExcelLibraryCreation.main(ExcelLibraryCreation.java:24)
    Java Result: 1 

I did more research on this problem on Stack Overflow and other sites, but the answers were extremely confusing to me because I’ve never heard of OLE2 files and header signatures and things of the sort. If someone could please explain the problem simply I would really appreciate it. Here is my code:

    public void processFile(File excelWorkbook) throws FileNotFoundException, IOException{
         System.out.println("Processing file...");
         FileInputStream fileInputStream = new FileInputStream(excelWorkbook);
         HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
         HSSFSheet firstSheet = workbook.getSheetAt(0);
         Iterator<Row> rowIterator = firstSheet.iterator();
          while (rowIterator.hasNext()){
               Row row = rowIterator.next();

               Iterator<org.apache.poi.ss.usermodel.Cell> cellIterator = row.cellIterator();

             while(cellIterator.hasNext()){
               org.apache.poi.ss.usermodel.Cell cell = cellIterator.next();
               switch(cell.getCellType()){
                  case Cell.CELL_TYPE_BOOLEAN:
                      System.out.println("Cell type is boolean: "+cell.getBooleanCellValue());
                      break;
                  case Cell.CELL_TYPE_NUMERIC:
                      System.out.println("Cell type is numeric: "+cell.getNumericCellValue());
                      break;
                  case Cell.CELL_TYPE_STRING:
                      System.out.println("Cell type is String: "+cell.getStringCellValue());
                      break;
            }
            System.out.println("");
        }
        fileInputStream.close();
    }
}
How to&Answers:

Why use Apache POI for read a Comma-separated values file?

You can use opencsv.

CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

Answer:

If you are not trying to open a real .xls Excel file, HSSF of POI won’t help you at all. HSSF is used to open the 97 Excel format, .xls files. It won’t work for anything else. Take Paul Vargas advise if you work with .csv files which are Comma-separated.