-
Open Workbook with Workbookfactory
@Test public void openworkbookbyworkbookfactory () { FileInputStream fileInputStream = null; workbook workbook = null; try { fileinputstream = new fileinputstream ("D:\\test.xls"); Workbook = workbookfactory.create (FileInputStream); //get the first sheet name system.out.println (workbook.getsheetname (0)); //Get Total Sheet System.out.println (WORKBOOK.GEtnumberofsheets ()); } catch (FileNotFoundException e) { e.printstacktrace (); } catch (invalidformatexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } finally { try { Fileinputstream.close (); } catch (ioexception e) { &nbsP; e.printstacktrace (); } } }
Second, open workbook with Poifsfilesystem
@Test public void openworkbookbypoifsfilesystem () { FileInputStream fileInputStream = null; workbook workbook = null; try { fileinputstream = new fileinputstream ("D:\\test.xls"); Poifsfilesystem poifsfilesystem = new poifsfilesystem (FileInputStream); workbook = new hssfworkbook ( Poifsfilesystem); //formulated sheet Name also back to sheet index System.out.println (Workbook.getsheetindex ("three")); } catch (filenotfoundexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } finally { try { Fileinputstream.close (); } catch (ioexception e) { e.printstacktrace (); } } }
Third, open the encrypted workbook (read encryption)
@Test     PUBLIC VOID OPERWORKBOOKOFSN () { try { fileinputstream fileinputstream = new fileinputstream ("D:\\test.xls"); BufferedInputStream bufferedInputStream = new Bufferedinputstream (FileInputStream); Poifsfilesystem poifsfilesystem = new poifsfilesystem (Bufferedinputstream); biff8encryptionkey.setcurrentuserpassword (" Password "); hssfworkbook Hssfworkbook = new hssfworkbook (Poifsfilesystem); system.out.println ("====================encryptedworkbook===================="); system.out.println ("Number of sheets:" + Hssfworkbook.getnumberofsheets ()); system.out.println ("Sheet0 ' S name:" + hssfworkbook.getsheetname (0)); } catch (filenotfoundexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } }
Java read and write Excel's POI chapter-003