Java Development POI Read import Excel file and verify

Source: Internet
Author: User

Apache POI is an open source, cross-platform Java API developed by Apache that provides APIs to Java programs for various operations in Microsoft Office format archives.

The Excel operation in POI is simple, the main class has

  • Hssfworkbook:Excel file
  • Hssfsheet:pagination sheet in Excel files
  • Hsshrow: Yes
  • Hssfcell: Cell

We want to import the data that reads and validates the cell, as follows:

Excel content:

Development examples:

ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.text.DecimalFormat;Importjava.text.ParseException;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;ImportOrg.apache.struts.upload.FormFile; Public classPoiimport {//import and validate files File_import.xls     Public Static voidMain (string[] args) {//To import a file with struts://formfile formfile = Batchchangeagform.getxlsfile (); //                //if (0 = = Formfile.getfilesize ()) {//this.setpromptmessage (Request, "The file selected is wrong!")        "); //return Mapping.findforward ("Batchchangeagimport"); // }        //InputStream is = Formfile.getinputstream (); //read files directly:String FilePath = "D:\\file_import.xls"; File File=NewFile (FilePath);        InputStream is;        Hssfsheet Sheetmain; Try{ is=Newfileinputstream (file); Poifsfilesystem FS=NewPoifsfilesystem (IS); Hssfworkbook WB=NewHssfworkbook (FS); //read the first sheetSheetmain = Wb.getsheetat (0);            Is.close (); //total number of rows            intRowlens =Sheetmain.getlastrownum (); intCollens = 8; interrcnt = 0; Hssfrow Row=NULL; Hssfcell Cell=NULL; String content= "";  for(intRowCount = 1; RowCount <= Rowlens; rowcount++) {System.out.println ("Read line:" +rowCount); Row=Sheetmain.getrow (RowCount); if(Row! =NULL) {                     for(intColCount = 0; ColCount < Collens; colcount++) {System.out.print ("Line:" + RowCount + "; Column:" +ColCount+ "Content:"); Cell= Row.getcell (( Short) colcount); Content=Getcellvalue (cell). Trim (); if(Content = = "") {System.out.println ("# # # found empty exception # # #"); } Else{System.out.println (content); }                    }                }            }        } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }     Public StaticString getcellvalue (Hssfcell cell) {if(Cell! =NULL) {            Switch(Cell.getcelltype ()) { CaseHssfcell.cell_type_blank:return"";  CaseHSSFCell.CELL_TYPE_NUMERIC:String strvalue=string.valueof (Cell.getnumericcellvalue ()); if(strvalue! =NULL&& strvalue.indexof (".")! =-1 && strvalue.indexof ("E")! =-1) {                    Try {                        return NewDecimalFormat (). Parse (strvalue). toString (); } Catch(ParseException e) {e.printstacktrace (); }                } Else {                    if(Strvalue.endswith (". 0")) {                        returnStrvalue.substring (0, Strvalue.indexof (". 0")); } Else {                        returnstrvalue; }                }             Casehssfcell.cell_type_string:return(Cell.getstringcellvalue () + ""). Trim ();  CaseHssfcell.cell_type_formula:return(Cell.getcellformula () + ""). Trim ();  CaseHssfcell.cell_type_boolean:returnCell.getbooleancellvalue () + "";  CaseHssfcell.cell_type_error:returnCell.geterrorcellvalue () + ""; }        }        return""; }}

Output:

Read line: 1Line:1; column: 0 of content: A000079 line:1; column: 1 of content: 000002017106088Line:1; column: 2 of content: 2Line:1; column: 3 of content: 2Line:1; column: 4 of content: 10000Line:1; column: 5 of content: 1000Line:1; column: 6 of content: 20171020Line:1; column: 7 content: Read rows issued:2Line:2; column: 0 of content: A000080 line:2; column: 1 of content: 000002018107088Line:2; column: 2 of content: 1Line:2; column: 3 of content: 1Line:2; column: 4 of content: 20000Line:2; column: 5 of content: 2000Line:2; column: 6 of content: 20181020Line:2; column: 7 content: Outgoing Read line:3Line:3; column: 0 of content: A000081 line:3; column: 1 of content: 000002018107099Line:3; column: 2 of content: 1Line:3; column: 3 of content: 1Line:3; column: 4 content: # # # found empty exception # # #行:3; column: 5 of content: 3000Line:3; column: 6 of content: 20181020Line:3; Column: 7 content: Outgoing

Java Development POI Read import Excel file and verify

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.