The second of the POI Tutorial: Create a time-formatted cell, work with cells of different content formats, traverse the rows and columns of the workbook, and get the contents of the cell, text extraction

Source: Internet
Author: User

Second Lecture 1. Create a cell of a time format

Workbook wb=NewHssfworkbook ();//to define a new workbookSheet Sheet=wb.createsheet ("first sheet page");//Create a first sheet page//first CellRow Row=sheet.createrow (0);//Create a rowcell Cell=row.createcell (0);//Create a cell 1th columnCell.setcellvalue (NewDate ());//set a value for a cell//Second CellCreationhelper Createhelper=Wb.getcreationhelper (); CellStyle CellStyle=wb.createcellstyle ();//cell style classCellstyle.setdataformat (Createhelper.createdataformat (). GetFormat ("Yyy-mm-dd Hh:mm:ss")); Cell=row.createcell (1);//second columnCell.setcellvalue (NewDate ());             Cell.setcellstyle (CellStyle); //a third cellCell=row.createcell (2);//third columnCell.setcellvalue (Calendar.getinstance ());             Cell.setcellstyle (CellStyle); //last written on the hard driveFileOutputStream fileout=NewFileOutputStream ("c:\\ workbook. xls");       Wb.write (fileout); Fileout.close ();

The effect is as follows:

Because the space is not enough, so the display does not come out, stretching a bit can be.

2. Handling cells in different content formats

Workbook wb=NewHssfworkbook ();//to define a new workbookSheet Sheet=wb.createsheet ("first sheet page");//Create a first sheet pageRow Row=sheet.createrow (0);//Create a rowcell Cell=row.createcell (0);//Create a cell 1th columnCell.setcellvalue (NewDate ());//set a value for a cellRow.createcell (1). Setcellvalue (1); Row.createcell (2). Setcellvalue ("a string"); Row.createcell (3). Setcellvalue (true); Row.createcell (4). Setcellvalue (Hssfcell.cell_type_numeric); Row.createcell (5). Setcellvalue (false); FileOutputStream Fileout=NewFileOutputStream ("c:\\ workbook. xls");        Wb.write (fileout); Fileout.close (); 

The effect is as follows:

3. Iterate through the rows and columns of the workbook and get the contents of the cells

//creating a tabular fileInputStream is=NewFileInputStream ("c:\\ two list. xls"); //Read FilePoifsfilesystem FS=NewPoifsfilesystem (IS); Hssfworkbook WB=NewHssfworkbook (FS); Hssfsheet Hssfsheet=wb.getsheetat (0);//get the first sheet page            if(hssfsheet==NULL){            return; }               //traversing rows row row         for(intRownum=0;rownum<=hssfsheet.getlastrownum (); rownum++) {Hssfrow Hssfrow=Hssfsheet.getrow (RowNum); if(hssfrow==NULL){                Continue; }            //traversing a column 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 ()); }    }

Note that it must be more than 2007 versions. 2010 words may not be recognized.

List of two goods. xls

Console read out:

4. Text Extraction

        InputStream is=New fileinputstream ("c:\\ two list. xls");        Poifsfilesystem fs=new  Poifsfilesystem (IS);        Hssfworkbook WB=new  Hssfworkbook (FS);               Excelextractor excelextractor=new  excelextractor (WB);        Excelextractor.setincludesheetnames (false); // We don't need the name of the sheet page.         System.out.println (Excelextractor.gettext ());

The effect is as follows:

The above source code is as follows: Http://yunpan.cn/cdBADQY7jS6WP access password E5CF

The second of the POI Tutorial: Create a time-formatted cell, work with cells of different content formats, traverse the rows and columns of the workbook, and get the contents of the cell, text extraction

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.