Work history: Import data from Excel using POI (Version 2003, 2007, and above)

Source: Internet
Author: User

use poi to read The data instances of version and above :
Public list<kqrecord> readxlsx (String path)  throws IOException {         inputstream is = new fileinputstream (path);         XSSFWorkbook xssfWorkbook = new  Xssfworkbook (IS);//2007 or above version         KqRecord kq = null;         list<kqrecord> list = new arraylist <KqRecord> ();        // read the sheet         for  (int numsheet = 0; numsheet <  xssfworkbook.getnumberofsheets ();  numsheet++)  {             xssfsheet xssfsheet = xssfworkbook.getsheetat (NumSheet);             if  (xssfsheet == null)  {                 continue;             }            //  Read the Row            for  ( Int rownum = 1; rownum <= xssfsheet.getlastrownum ();  rowNum++)  {                 XSSFRow  Xssfrow = xssfsheet.getrow (RowNum);                 if  (xssfrow != null)  {                     kq = new  kqrecord ();                     //one-to-one reading                      xssfcell id = xssfrow.getcell (0);                     XSSFCell Kqid =  Xssfrow.getcell (1);                     xssfcell deptname = xssfrow.getcell (2);                     XSSFCell  Devicenmae = xssfrow.getcell (3);                     xssfcell name = xssfrow.getcell (4);      &nbSp;              xssfcell checktime  = xssfrow.getcell (5);                     xssfcell checktype = xssfrow.getcell (6);                      Xssfcell validateflag = xssfrow.getcell (7);                     XSSFCell SnId =  Xssfrow.getcell (8);                     //Object Setting Value                      kq.setid (GetValue (Id));                      kq.setkqid (GetValue (kqid));                     kq.setdeptname (GetValue (deptname));                     kq.setdevicenmae (GetValue (devicenmae));                     kq.setname (GetValue ( Name));                     kq.setchecktime (GetValue (checktime));                     kq.setchecktype (GetValue (CheckType));                      kq.setvalidateflag (GetvAlue (Validateflag));                     kq.setsnid (GetValue (SnId));                     list.add (KQ);                 }             }        }         return list;    }     @SuppressWarnings (" Static-access ")     private string getvalue (Xssfcell xssfrow)  {         if  (Xssfrow.getcelltype ()  == xssfrow.cell_type_ BOOLEAN)  {            return  String.valueof (Xssfrow.getbooleancellValue ());        } else if  (Xssfrow.getcelltype ()   == xssfrow.cell_type_numeric)  {             return string.valueof (Xssfrow.getnumericcellvalue ());         } else {            return  String.valueof (Xssfrow.getstringcellvalue ());        }     }

Test instance:

@Test     public void testreadexcel () {         try {            List<KqRecord>  readexcel = readexcel ("d:/attendance record sheet. xlsx");             system.out.println (Readexcel.size ());             system.out.println (readexcel);         } catch   (ioexception e)  {             E.printstacktrace ();        }    }         public list<kqrecord> readexcel (String path)   throws ioexception {        if  (StringUtils.isBlank ( Path))  {            return null;         } else {            return &NBSP;READXLSX (path);         }    }
use poi to read An instance of the 2003 version of Excel data
Public list<kqrecord> readxls (String path)  throws IOException {         inputstream is = new fileinputstream (path);         HSSFWorkbook hssfWorkbook = new  Hssfworkbook (IS);        kqrecord kq = null;         List<KqRecord> list = new ArrayList< Kqrecord> ();        // read the sheet         for  (int numsheet = 0; numsheet <  Hssfworkbook.getnumberofsheets ();  numsheet++)  {             hssfsheet hssfsheet = hssfworkbook.getsheetat (NumSheet);             if  (hssfsheet == null)  {                 continue;             }            // Read  the row            for  (Int rowNum  = 1; rownum <= hssfsheet.getlastrownum ();  rownum++)  {                 hssfrow hssfrow =  hssfsheet.getrow (RowNum);                 if  (hssfrow != null)  {                     kq = new kqrecord ();      &Nbsp;              hssfcell no  = hssfrow.getcell (0);                     hssfcell name = hssfrow.getcell (1);                     hssfcell  age = hssfrow.getcell (2);                     hssfcell score = hssfrow.getcell (3);                      kq.setid (GetValue (NO));                     kq.setkqid (GetValue (name));           &nbSp;         kq.setdeptname (GetValue (age));                      Kq.setdevicenmae (GetValue (score));                     list.add (KQ);                 }             }        }         Return list;    }     private string getvalue ( Hssfcell hssfcell)  {        if  (Hssfcell.getcelltype ()  == hssfcell.cell_type_boolean)  {             return string.valueOf (Hssfcell.getbooleancellvalue ());        } else if  ( Hssfcell.getcelltype ()  == hssfcell.cell_type_numeric)  {             return string.valueof (Hssfcell.getnumericcellvalue ());         } else {             return string.valueof (Hssfcell.getstringcellvalue ());         }}

The test data is the same as before:

Reference Articles ; http://www.cnblogs.com/hongten/p/java_poi_excel_xls_xlsx.html

Work history: Import data from Excel using POI (Version 2003, 2007, and above)

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.