Excel read error: Invalid caption signature. How to allow?

I am downloading a single excel file from a browser. I am using a POI jar. But get an invalid header signature error; read 3255307777713450285, expected -2226271756974174256

below are the two jsp files that I 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 an error in the line POIFSFileSystem myFileSystem = new POIFSFileSystem (file);

How to solve this problem?

+6
source share
2 answers

You get this error because your file is not really an Excel.xls file, it is something else.

I suggest you try opening the file in Notepad and looking at it. Almost all errors of this type end up being renamed .csv or .html to .xls. Excel will gladly download html files (rendering tables as tables) and csv files and will not warn you that you have the wrong extension on them.

Please note that if you rename the .xlsx file to .xls and pass it the POI POIFSFileSystem or HSSFWorkbook, you will get a more specific error warning you that you received the .xlsx file instead. You get this error only because the POI absolutely does not know what your file is, only that it is not .xls

+9
source

Just ideal, if you are using maven, make sure that the filtering of resource tags is set to false. Otherwise, maven may corrupt xls files in the copy phase.

in your pom.xml

0
source

Source: https://habr.com/ru/post/910666/


All Articles