How does Java read data from an Excel file?

Source: Internet
Author: User
How to read data from an Excel file in Java
Package cn.com. spaceware. lixun;


Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. iterator;

Import org. Apache. Poi. hssf. usermodel. hssfcell;
Import org. Apache. Poi. hssf. usermodel. hssfrow;
Import org. Apache. Poi. hssf. usermodel. hssfsheet;
Import org. Apache. Poi. hssf. usermodel. hssfworkbook;

Import cn.com. spaceware. sxd. SC _bank_database.databean.repairinfobean;


/**
* Read the maintenance information of the ATM from the Excel file
*
* Author: shune Lee
*
* Date: November 22nd, 2005
*/

Public class readrepairinfo {

Private Boolean iscancel = false; // controls whether to continue reading.
Private string filepath = NULL; // full path for storing Excel files

/* Control the format variable */
Private int headnum = 2; // control the read from row headnum, because the row index starts from 0.
Private int leftnum = 0; // The control reads data from the leftnum column, because the column index starts from 0.


Readrepairinfo (string file ){
This. filepath = file;
}


/**
*
* @ Author shune Lee
*
* Todo reads maintenance information from the Excel file and adds it to repairinfobean.
*
*/
Public arraylist readexcel4repairinfo (){
Arraylist table = new arraylist ();
Arraylist record = new arraylist ();

Try {

Hssfworkbook workbook = new hssfworkbook (New fileinputstream (filepath ));
Hssfsheet sheet = Workbook. getsheetat (0); // get a sheet

/* Obtain the number of imported records, that is, the number of data rows */
Int rownum = (sheet. getlastrownum ()-sheet. getfirstrownum () + 1)-headnum; // The row index starts from 0.

/* Traverse all the data and pass the obtained data to rinfo */
For (INT I = headnum; I
/* If you set iscancel to true, stop reading */
If (iscancel)
Break;

Hssfrow ROW = sheet. getrow (I );

For (short J = row. getfirstcellnum (); j
String cellcontent1 = NULL;
Double cellcontent2 = NULL;
Hssfcell cell = row. getcell (j );

/* Determine the cell content type and assign it to the corresponding variable */
If (cell. getcelltype () = 1 ){
Cellcontent1 = cell. getstringcellvalue ();
Record. Add (cellcontent1 );
}
Else if (cell. getcelltype () = 0 ){
Cellcontent2 = new double (cell. getnumericcellvalue ());
Record. Add (cellcontent2 );
}
Else
System. Out. println ("there is a problem with the data in Excel. Please check. ");
}
Table. Add (getrepairinfobean (record ));
}

} Catch (filenotfoundexception e ){
System. Out. println ("file not found" + filepath );
E. printstacktrace ();
} Catch (ioexception e ){
System. Out. println ("File Access Error" + filepath );
E. printstacktrace ();
}

Return table;
}

/**
*
* @ Author shune Lee
*
* Todo uses records read from Excel to generate repairinfobean
*
* @ Param arraylist one record read from Excel, that is, one row
*/
Public repairinfobean getrepairinfobean (arraylist content ){
Repairinfobean rpinfo = new repairinfobean ();

If (content! = NULL &&! (Content. isempty ())){

/* Input data to the repairinfobean object */
Rpinfo. setrepairid (double) content. Get (0). intvalue ());
Rpinfo. setstratmendcode (double) content. Get (1). intvalue ());
Rpinfo. setdeployid (double) content. Get (2). intvalue ());
Rpinfo. setstrrepairtype (double) content. Get (3). intvalue ());
Rpinfo. setstrcontent (string) content. Get (4 ));
Rpinfo. setstrpersonname (string) content. Get (5 ));
Rpinfo. setstrdate (new string (). valueof (content. Get (6 )));
System. Out. println ("the size is =" + content. Size ());
}

Return rpinfo;
}

/* Test function */
Public static void main (string [] ARGs ){
Readrepairinfo = new readrepairinfo ("E: // test.xls ");

Arraylist list = readrepairinfo. readexcel4repairinfo ();

Iterator it = List. iterator ();
While (it. hasnext ()){
Repairinfobean rpinfo = (repairinfobean) it. Next ();
System. Out. println ("repairid =" + rpinfo. getrepairid () + ";" + "strcontent =" + rpinfo. getstrcontent ());
}
}

} 

Related Article

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.