POI Basic Operation

Source: Internet
Author: User

1. Read Excel file

NewNew New Hssfworkbook (FS); 
Hssfsheet sheet = wb.getsheetat (0);

2. Get the total number of rows

int rows = Sheet.getlastrownum ();

3. Create a workbook

New Hssfworkbook (); // to define a new workbook New FileOutputStream ("D:\\poidemo.xls"); Wb.write (fileout); Fileout.close ();

4. Create Sheet page

New Hssfworkbook (); // Define a new workbook Wb.createsheet ("Firstsheet"); // Create a sheet page wb.createsheet ("Secondsheet"); // Create a second sheet page New FileOutputStream ("D:\\poisheetdemo.xls");

5. Create a cell

Workbook WB =NewHssfworkbook (); Sheet Sheet= Wb.createsheet ("Firstsheet"); Row Row= Sheet.createrow (0);//Create a rowCell cell = Row.createcell (0);//Create a cellCell.setcellvalue (1); Row.createcell (1). Setcellvalue (1.2);//Create a cell second column with a value of 1.2Row.createcell (2). Setcellvalue ("This is a string");//Create a cell third column with a value of 1.2FileOutputStream fileout=NewFileOutputStream ("D:\\poisheet.xls");        Wb.write (fileout); Fileout.close ();

6. Traverse Workbook

 Public Static voidMain (string[] args)throwsException {InputStream is=NewFileInputStream ("d:\\ traversal list. xls"); Poifsfilesystem FS=NewPoifsfilesystem (IS); Hssfworkbook WB=NewHssfworkbook (FS); Hssfsheet Hssfsheet= Wb.getsheetat (0); if(hssfsheet==NULL){            return; }        //traversing rows row row         for(intRowNum = 0;rownum<=hssfsheet.getlastrownum (); rownum++) {Hssfrow Hssfrow=Hssfsheet.getrow (RowNum); if(hssfrow==NULL){                Continue; }            //Traverse Split Cell             for(intCellnum = 0;cellnum<=hssfrow.getlastcellnum (); cellnum++) {Hssfcell Hssfcell=Hssfrow.getcell (Cellnum); if(hssfcell==NULL){                    Continue; } System.out.print (" "+GetValue (Hssfcell));        } System.out.println (); }    }        Private StaticString getValue (Hssfcell hssfcell) {if(Hssfcell.getcelltype () = =Hssfcell.cell_type_boolean) {            returnstring.valueof (Hssfcell.getbooleancellvalue ()); }Else if(Hssfcell.getcelltype () = =hssfcell.cell_type_numeric) {            returnstring.valueof (Hssfcell.getnumericcellvalue ()); }Else{            returnstring.valueof (Hssfcell.getstringcellvalue ()); }    }

7. Text Extraction

New FileInputStream ("d:\\ traversal list. xls"newnew  Hssfworkbook (FS);     New excelextractor (WB); Excelextractor.setincludesheetnames (false); // don't need the name of the sheet page System.out.println (Excelextractor.gettext ());

8. Cell Merging

New Hssfworkbook ();         = Wb.createsheet ("Sheet1");         = Sheet.createrow (1);                 = Row.createcell (1);        Cell.setcellvalue ("cell merge");                Sheet.addmergedregion (new cellrangeaddress (1,2,1,2)); // start line, end row, start column, end column

9. Read and rewrite workbooks

InputStream INP =NewFileInputStream ("D:\\demo13.xls"); Poifsfilesystem FS=NewPoifsfilesystem (INP); Workbook WB=NewHssfworkbook (FS); Sheet Sheet= Wb.getsheetat (0);//get the first sheetRow row = Sheet.getrow (0); Cell Cell=row.getcell (0); if(cell==NULL) {cell= Row.createcell (3);        } cell.setcelltype (cell.cell_type_string); Cell.setcellvalue ("Test Cell"); FileOutputStream Fileout=NewFileOutputStream ("D:\\demo13.xls");        Wb.write (fileout); Fileout.close ();

POI Basic Operation

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.