Java read and write Excel's POI chapter-002

Source: Internet
Author: User

poi Create Excel file
    • Create a suffix. xls Excel file

     @Test     public void testcreateworkbook_xls ()  {         HSSFWorkbook hssfWorkbook = new  Hssfworkbook ();         //Add worksheet (the XLS file generated when sheet is not added will error when opened)          hssfsheet sheetone = hssfworkbook.createsheet ();         hssfsheet sheettwo = hssfworkbook.createsheet () ;        hssfsheet sheetthree =  Hssfworkbook.createsheet ("three");         //Save as Excel file, xls suffix          FileOutputStream fileOutputStream = null;         String filePath =  "D:\\test.xls";         try {     &Nbsp;      fileoutputstream = new fileoutputstream (FilePath);             hssfworkbook.write (FileOutputStream);         } catch  (filenotfoundexception e)  {             e.printstacktrace ();             system.out.println (E.tostring ());         } catch  (ioexception e)  {             e.printstacktrace ();             system.out.println (E.tostring ());        }  finally {            try {        &nBsp;        fileoutputstream.close ();             } catch  (ioexception e)  {                 e.printstacktrace ();                 system.out.println ( E.tostring ());            }         }    }

    • Create a suffix. xlsx Excel file

    //Generate Workbook ooxml form (. xlsx)      @Test      Public void testcreateworkbook_xlsx () {         // Save as Excel file, xlsx suffix         String filePath =  "d:\\ Test.xlsx ";        fileoutputstream fileoutputstream =  null;        xssfworkbook xssfworkbook = new  Xssfworkbook ();        xssfsheet xssfsheetone =  Xssfworkbook.createsheet ();         xssfsheet xssfsheettwo =  xssfworkbook.createsheet (";        try {  ")           fileOutputStream = new  FileOutputStream (FilePath);            xssfworkbook.write (FileOutputStream);         } catch  (filenotfoundexception e)  {             e.printstacktrace ();         } catch  (ioexception e)  {             e.printstacktrace ();        } finally {             try {                 fileoutputstream.close ();             } catch  (ioexception e)  {                 e.printstacktrace ();             }        }     }

Use version poi-3.10.1

Relatively simple, not much to say!

Java read and write Excel's POI chapter-002

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.