Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel read error : Invalid header signature. How to resolve?

I am uploading one excel file from browser. I am using POI jar. But getting error Invalid header signature; read 3255307777713450285, expected -2226271756974174256

below the two jsp files i have used: JSP 1:

<form action="Upload.jsp" enctype="MULTIPART/FORM-DATA" method=post >
  <input type="file" name="filename" />
  <input type="submit" value="Upload" />
</form>

JSP 2:Upload.jsp

try{
        InputStream file = request.getInputStream();
        POIFSFileSystem myFileSystem = new POIFSFileSystem(file);
        HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
        HSSFSheet mySheet = myWorkBook.getSheetAt(0);
        Iterator rowIter = mySheet.rowIterator();
        rowIter.next(); 
        while (rowIter.hasNext()) {
            HSSFRow myRow = (HSSFRow) rowIter.next();
            Iterator cellIter = myRow.cellIterator();
            cellIter.next();
            System.out.println(((HSSFCell)cellIter.next()).toString());
        }


    }catch(Exception ex){
        System.out.println(ex.getMessage());
    }

But getting the error at line POIFSFileSystem myFileSystem = new POIFSFileSystem(file);

How to resovle this problem?

like image 523
Sweet Dream Avatar asked Mar 14 '12 06:03

Sweet Dream


1 Answers

You're getting this error because your file isn't actually an Excel .xls file, it's something else.

I'd suggest you try opening the file in Notepad, and look at it there. Almost all errors of this type end up being that a .csv or .html file has been renamed to .xls. Excel will happily load html files (rendering the table as the spreadsheet) and csv files, and doesn't warn you that you've got the wrong extension on them.

Note that if you rename a .xlsx file to a .xls and pass that to POI's POIFSFileSystem or HSSFWorkbook, you'll get a more specific error warning you that you've got a .xlsx file instead. You're only getting this error because POI has absolutely no idea what your file is, only that it's not a .xls

like image 132
Gagravarr Avatar answered Nov 11 '22 11:11

Gagravarr