Java Excel 读取错误:标题签名无效。如何解决?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9696836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel read error : Invalid header signature. How to resolve?
提问by Sweet Dream
I am uploading one excel file from browser. I am using POI jar. But getting error Invalid header signature; read 3255307777713450285, expected -2226271756974174256
我正在从浏览器上传一个 excel 文件。我正在使用 POI 罐子。但是得到错误 Invalid header signature; 读取 3255307777713450285,预期 -2226271756974174256
below the two jsp files i have used: JSP 1:
在我使用过的两个 jsp 文件下面: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
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);
但是在 PIOFSFileSystem myFileSystem = new PIFSFileSystem(file); 行出现错误
How to resovle this problem?
如何解决这个问题?
回答by Gagravarr
You're getting this error because your file isn't actually an Excel .xls file, it's something else.
您收到此错误是因为您的文件实际上不是 Excel .xls 文件,而是其他文件。
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.
我建议您尝试在记事本中打开该文件,然后在那里查看。几乎所有此类错误最终都是 .csv 或 .html 文件已重命名为 .xls。Excel 会很高兴地加载 html 文件(将表格呈现为电子表格)和 csv 文件,并且不会警告您使用了错误的扩展名。
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
请注意,如果您将 .xlsx 文件重命名为 .xls 并将其传递给 POI 的 POIFSFileSystem 或 HSSFWorkbook,您将收到更具体的错误警告,警告您使用的是 .xlsx 文件。您只会收到此错误,因为 POI 完全不知道您的文件是什么,只知道它不是 .xls
回答by PRAWIN
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
只是一个想法,如果您使用 maven,请确保在资源标签过滤中设置为“ false”。否则 maven 往往会在复制阶段损坏 xls 文件
in your pom.xml
在你的 pom.xml