Java read and write Excel's POI chapter-003

Source: Internet
Author: User

    1. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.