Java Read Excel file

Source: Internet
Author: User

1.poi Mode Read parsing

Excel file:

 PackageCom.mi.util;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.text.SimpleDateFormat;Importjava.util.ArrayList;Importjava.util.List;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.ss.usermodel.Cell;Importcom.mi.entity.Student; Public classPoireadexceltool { Public Static voidMain (string[] args)throwsException {List<Student> list =Readxls ();  for(Student stu:list) {System.out.println (Stu.getid ());            System.out.println (Stu.getname ());            System.out.println (Stu.getage ());        System.out.println (Stu.getbirth ()); }    }     Public StaticList<student> Readxls ()throwsException {InputStream is=NewFileInputStream ("D:/student.xls"); Hssfworkbook Excel=NewHssfworkbook (IS); Student Stu=NULL; List<Student> list =NewArraylist<student>(); //Looping Worksheets Sheet         for(intNumsheet = 0; Numsheet < Excel.getnumberofsheets (); numsheet++) {Hssfsheet sheet=Excel.getsheetat (Numsheet); if(Sheet = =NULL)                Continue; //Looping rows Row             for(intRowNum = 1; RowNum < Sheet.getlastrownum (); rownum++) {Hssfrow row=Sheet.getrow (RowNum); if(Row = =NULL)                    Continue; Stu=NewStudent (); //Cyclic column cell//0 School Number 1 Name 2 year old 3 birthday                /*System.out.println ((int) cell0.getnumericcellvalue ());                System.out.println (Cell1.getstringcellvalue ());                SYSTEM.OUT.PRINTLN ((int) cell2.getnumericcellvalue ()); System.out.println (Cell3.getstringcellvalue ());*/                /**Console: * 1 316 1997-0                 3-12 2 Lee 417 1996-08-12 */Hssfcell cell0= Row.getcell (0); if(Cell0 = =NULL)                    Continue; Stu.setid ((int) Cell0.getnumericcellvalue ()); Hssfcell Cell1= Row.getcell (1); if(Cell1 = =NULL)                    Continue;                Stu.setname (Cell1.getstringcellvalue ()); Hssfcell cell2= Row.getcell (2); if(Cell2 = =NULL)                    Continue; Stu.setage ((int) Cell2.getnumericcellvalue ()); Hssfcell Cell3= Row.getcell (3); if(Cell3 = =NULL)                    Continue; Stu.setbirth (NewSimpleDateFormat ("Yyyy-mm-dd"). Parse (Cell3.getstringcellvalue ()));            List.add (Stu); }        }        returnlist; } @SuppressWarnings ("Unused")    Private StaticString getValue (Hssfcell cell) {if(Cell.getcelltype () = =Cell.cell_type_boolean) {            //Returns a Boolean type value            returnstring.valueof (Cell.getbooleancellvalue ()); } Else if(Cell.getcelltype () = =cell.cell_type_numeric) {            //returns the value of a numeric type            returnstring.valueof (Cell.getnumericcellvalue ()); } Else {            //returns the value of a string type            returnCell.getstringcellvalue (); }    }}

Results:

1
Tom
16
Sun Jan 00:03:00 CST 1997
2
John doe
17
Fri Jan 00:08:00 CST 1996

Java Read Excel file

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.