Package used: Poi-ooxml-3.14.jar; Poi-ooxml.jar;
1. To find the relevant files online, the most answer is to use Hssfworkbook to read:
Poifsfilesystem Poifsfilesystem = new Poifsfilesystem (new FileInputStream (file)); Hssfworkbook workbook = new Hssfworkbook (Poifsfilesystem);
result : The xlsx file cannot be identified;
2. Specifically for the xlsx file find solutions, get used Xssfworkbook to deal with;
result : xlsx files can be identified;
3. Need to determine the document, what situation with hssfworkbook, under what circumstances with Xssfworkbook
if (!inp.marksupported ()) INP = new Pushbackinputstream (INP, 8); if (Npoifsfilesystem.haspoifsheader ( Ioutils.peekfirst8bytes (INP))) {Npoifsfilesystem fs = new Npoifsfilesystem (INP);} else if (Poixmldocument.hasooxmlheader (INP)) return new Xssfworkbook (Opcpackage.open (INP)); else throw new Invalidformatexception ("Your Excel version is not currently available for POI Resolution");
OK, seemingly perfect solution to the problem, but in fact, in the Workbookfactory has already had the related operation method to Excel
4. Borrowing Tool class Workbookfactory
/** * Creates the appropriate hssfworkbook/xssfworkbook from * The given inputstream. * Your input stream must either support mark/reset, or ' be wrapped as a {@link pushbackinputstream}! * /public static Workbook Create (InputStream inp) throws IOException, invalidformatexception { //If clearly does N ' t do mark/reset, wrap up if (! inp.marksupported ()) { INP = new Pushbackinputstream (INP, 8); } if (Poifsfilesystem.haspoifsheader (INP)) { return new Hssfworkbook (INP); } if (Poixmldocument.hasooxmlheader (INP)) { return new Xssfworkbook (Opcpackage.open (INP)); } throw new IllegalArgumentException ("Your InputStream was neither a OLE2 stream, nor an OOXML stream"); }
5. Ultimate Edition
Workbook Workbook = workbookfactory.create (new FileInputStream (file));
A line of code to fix, ^_^;
Java Read Excel file