Java – Excel read error: invalid header signature How?

I am uploading an excel file from my browser I'm using poi. Jar But get wrong

The two JSP files I use are as follows: 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());
    }

However, an error is obtained in the poifsfilesystem myfile system = new poifsfilesystem (file);

How to solve this problem?

Solution

You received this error because your file is not actually excel Xls file, but other content

I suggest you try to open the file in Notepad and view it there Almost all such errors end up being CSV or The HTML file has been renamed to xls. Excel will be happy to load HTML files (rendering tables as spreadsheets) and CSV files without warning you that they have wrong extensions

Please note that if Rename the xlsx file to Xls and pass it to the poifsfilesystem or hssfworkbook of the POI, you will receive a more specific error warning that you have obtained Xlsx file You just receive this error because POI has no idea what your file is, but it is not xls

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>