POI Usage Explained

Source: Internet
Author: User
Tags table name microsoft outlook
1.POI structure and common class POI Reading table cannot be more than 65535 rows
(1) POI IntroductionApache POI is an open source project for the Apache Software Foundation, where POI provides APIs to Java programs to read and write to Microsoft Office format archives. NET developers can take advantage of Npoi (POI for. net) to access the capabilities of Microsoft Office documents. (2) POI structure Description Package Name DescriptionHSSF provides the ability to read and write Microsoft Excel xls format files. XSSF provides the ability to read and write Microsoft Excel OOXML xlsx format files. HWPF provides the ability to read and write Microsoft Word doc format files. HSLF provides the ability to read and write Microsoft PowerPoint format files. HDGF provides the ability to read Microsoft Visio format archives. HPBF provides the ability to read Microsoft Publisher format files. HSMF provides the ability to read Microsoft Outlook format files. (3) POI Common class description Class Name DescriptionHssfworkbook Excel Document object Hssfsheet Excel form
Hssfrow lines of Excel
Hssfcell Excel's lattice unit
Hssffont Excel Font Hssfdataformat The date format of the grid cell Hssfheader Excel document sheet header hssffooter Excel document S Heet footer Hssfcellstyle grid cell style hssfdateutil Date Hssfprintsetup print hssferrorconstants error information table back to top basic operation of 2.Excel (1) Create workbook and sheet
       
       
        
        publicclassTest00
        
        {
        
        publicstaticvoid main (string[] args) throwsioexception
        
        {
        
        String filepath= "d:\\ users\\lizw\\ desktop \\POI\\sample.xls ";//file path
        
        Hssfworkbook workbook =newhssfworkbook ();//create Excel File (workbook)
        
        Hssfsheet sheet = workbook.createsheet ();//Create worksheet (sheet)
        
        		sheet = workbook.createsheet ("Test");//create sheet (sheet)
        
        FileOutputStream out =newfileoutputstream (filePath);
        
        		Workbook.write (out);//save Excel File
        
        		out.close ();//close file stream
        
        System.out.println ("ok!");}
        
        }
       
       
(2) Create a cell
       
       
        
        Hssfsheet sheet = workbook.createsheet ("Test");//Create worksheet (sheet)
        
        hssfrow row = sheet.createrow (0);//Create line, starting
        
        from 0 Hssfcell cell = Row.createcell (0);//The cell that created the row is also starting from 0
        
        cell.setcellvalue ("Li Zhiwei");//Set cell contents
        
        Row.createcell (1). Setcellvalue (false);//Set cell contents, Overload
        
        Row.createcell (2). Setcellvalue (Newdate ());//Set cell contents, overload
        
        Row.createcell (3). Setcellvalue (12.345);//Setting cell contents, overloading
       
       
(3) Create document summary information
       
       
        
        Workbook.createinformationproperties ();//Create Document Information
        
        documentsummaryinformation dsi= Workbook.getdocumentsummaryinformation ();//summary Information
        
        dsi.setcategory ("Category: Excel File");//category
        
        Dsi.setmanager ("Manager : Li Zhiwei ");//manager
        
        Dsi.setcompany (" Company:--");//company
        
        summaryinformation si = workbook.getsummaryinformation ();//summary information
        
        Si.setsubject ("Subject:-");//Theme
        
        Si.settitle ("title: Test Document");//title
        
        Si.setauthor ("Author: Li Zhiwei");//author
        
        Si.setcomments ("Note: Poi test document");//Notes
       
       
(4) Creating annotations
       
       
        
        Hssfsheet sheet = workbook.createsheet ("Test");//create sheet (sheet)
        
        hssfpatriarch patr = Sheet.createdrawingpatriarch ( );
        
        Hssfclientanchor anchor = Patr.createanchor (0,0,0,0,5,1,8,3);//create annotation position
        
        hssfcomment comment = Patr.createcellcomment (anchor);//Create Annotation
        
        comment.setstring (newhssfrichtextstring ("This is an annotated paragraph. );//Set annotation content
        
        comment.setauthor ("Li Zhiwei");//Set annotation author
        
        comment.setvisible (TRUE);//Set annotation default display
        
        Hssfcell cell = Sheet.createrow (2). Createcell (1);
        
        Cell.setcellvalue ("test");
        
        Cell.setcellcomment (comment);//assigning Annotations to cells
       
       
Create an annotation location Hssfpatriarch.createanchor (dx1, Dy1, DX2, Dy2, col1, Row1, col2, Row2) method parameter Description:
       
       
        
        DX1 the         x-axis offset in the 1th cell dy1 the offset of the         y-axis in the 1th cell
        
        dx2 the         x-axis offset in the 2nd cell dy2 the offset of the         y-axis in the 2nd cells
        
        col1        the 1th cell column number row1 the        row number of the 1th cell col2 the
        
        column number of the        2nd cell
        
        row2        The row number of the 2nd cell
       
       
(5) Creating headers and footers
       
       
        
        Hssfsheet sheet = workbook.createsheet ("Test");//create sheet (sheet)
        
        Hssfheader header =sheet.getheader ();//Get Header
        
        Header.setleft ("left of Header");
        
        Header.setright ("Right of Header");
        
        Header.setcenter ("middle of Header");
        
        Hssffooter footer =sheet.getfooter ();//Get Footer
        
        footer.setleft ("left footer");
        
        Footer.setright ("Right of Footer");
        
        Footer.setcenter ("middle of footer");
       
       
You can also use the label definitions from office, which you can access through Hssfheader or Hssffooter, which are static properties, listed below:
      
      
       
       Hssfheader.tab                  &a	table name
       
       

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.