Java Operation Excle POI (i)

Source: Internet
Author: User

Content: 1. Create Excel and different types of cells 2. Read Excel3. Text extraction

1.

 PackageCom.maya.poi;ImportJava.io.FileOutputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.ss.usermodel.Cell;ImportOrg.apache.poi.ss.usermodel.Row;ImportOrg.apache.poi.ss.usermodel.Sheet;ImportOrg.apache.poi.ss.usermodel.Workbook; Public classDemo3 { Public Static voidMain (string[] args)throwsException {Workbook WB=NewHssfworkbook ();//Define a workbook        /*Sheet Sheet=wb.createsheet ("First Sheet page");//Create a Sheet page row row=sheet.createrow (0);//create a row of cells Cell=row.cre        Atecell (0);//Create a cell cell.setcellvalue ("This is the first cell"); FileOutputStream out=new FileOutputStream ("c:\\ This is a poi out of Cell.xls");*/                //Analog LoopsSheet Sheet=wb.createsheet ("First Sheet page");//Create a sheet page         for(inti=0;i<3;i++) {row row=sheet.createrow (i);//Create a row             for(intj=0;j<10;j++) {cell cell=row.createcell (j);//Create a cellCell.setcellvalue ("+" + (i+1) + "line" + (j+1) + "column"); }} FileOutputStream out=NewFileOutputStream ("c:\\ This is Cell2.xls with POI");        Wb.write (out);    Out.close (); }}

2.

 PackageCom.maya.poi2;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportOrg.apache.poi.hssf.usermodel.HSSFCell;ImportOrg.apache.poi.hssf.usermodel.HSSFRow;ImportOrg.apache.poi.hssf.usermodel.HSSFSheet;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.poifs.filesystem.POIFSFileSystem; Public classDemo4 {//Read Excel     Public Static voidMain (string[] args)throwsException {InputStream in=NewFileInputStream ("F:\\ Dick and Harry. xls"); Poifsfilesystem FS=NewPoifsfilesystem (in); Hssfworkbook WB=NewHssfworkbook (FS); Hssfsheet Hssfsheet=wb.getsheetat (0);//get the first sheet page        if(hssfsheet==NULL){            return ; }        //Traverse Row         for(intRownum=0;rownum<=hssfsheet.getlastrownum (); rownum++) {Hssfrow Hssfrow=Hssfsheet.getrow (RowNum); if(hssfrow==NULL){                Continue; }            //traversing Columns             for(intCellnum=0;cellnum<=hssfrow.getlastcellnum (); cellnum++) {Hssfcell Hssfcell=Hssfrow.getcell (Cellnum); if(hssfcell==NULL){                    Continue; } System.out.print ("  "+GetValue (Hssfcell)); } System.out.println (""); }            }         Public 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 if(Hssfcell.getcelltype () = =Hssfcell.cell_type_formula) {            returnstring.valueof (Hssfcell.getcachedformularesulttype ()); }Else{            returnstring.valueof (Hssfcell.getstringcellvalue ()); }    }}

3.

 PackageCom.maya.poi2;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportOrg.apache.poi.hssf.extractor.ExcelExtractor;ImportOrg.apache.poi.hssf.usermodel.HSSFWorkbook;ImportOrg.apache.poi.poifs.filesystem.POIFSFileSystem; Public classDemo5 {//file Extraction     Public Static voidMain (string[] args)throwsException {InputStream in=NewFileInputStream ("F:\\ Dick and Harry. xls"); Poifsfilesystem FS=NewPoifsfilesystem (in); Hssfworkbook WB=NewHssfworkbook (FS); Excelextractor ex=NewExcelextractor (WB);//file Extraction//Ex.setincludesheetnames (false);//Do not extract sheet page namesSystem.out.println (Ex.gettext ()); }}

Java Operation Excle Poi (i)

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.