问题
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?
回答1:
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
回答2:
Just an idee, if you using maven make sure in the resource tag filtering is set to "false". Otherwise maven tends to corrupt xls files in the copying phase
in your pom.xml
来源:https://stackoverflow.com/questions/9696836/excel-read-error-invalid-header-signature-how-to-resolve