Java parse Excel

Source: Internet
Author: User
Tags rows count

Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. List;

Import org. Apache. log4j. Logger;
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;

/**
* Excel document parsing tools
* This tool parses tables in an Excel file into a data set composed of Java basic classes.
* The entire Excel table consists of multiple rows. Each row is represented by a list.
* Rows in Excel are represented by a list. The data indexes of each column are stored in the list one by one from 0;
* Multiple rows form the entire table and a list stores multiple rows.
*
**************************************** ***
* COM. trumptech. Common. fileparser. Excel
* 2007-6-15
* 16:20:38
* Author linfan
**************************************** ***
*/
Public class excelsheetparser {

Private logger = logger. getlogger (excelsheetparser. Class );

Private hssfworkbook workbook;

Public excelsheetparser (File excelfile) throws filenotfoundexception, ioexception {

Workbook = new hssfworkbook (New fileinputstream (excelfile ));
}

/**
* Obtain the data in the table.
* @ Param sheetnumber table index (Excel is a multi-Table document, so you need to enter the table index number)
* @ Return the rows and tables composed of lists
* @ Throws filenotfoundexception
* @ Throws ioexception
*/
Public list <list> getdatasinsheet (INT sheetnumber) throws filenotfoundexception, ioexception {
List <list> result = new arraylist <list> ();

// Obtain the specified table
Hssfsheet sheet = Workbook. getsheetat (sheetnumber );

// Obtain the total number of data rows
Int rowcount = sheet. getlastrownum ();
Logger.info ("found Excel rows count:" + rowcount );
If (rowcount <1 ){
Return result;
}

// Read data row by row
For (INT rowindex = 0; rowindex <= rowcount; rowindex ++ ){

// Obtain the row object
Hssfrow ROW = sheet. getrow (rowindex );

If (row! = NULL ){

List <Object> rowdata = new arraylist <Object> ();

// Obtain the number of cells in the row
Int columncount = row. getlastcellnum ();

// Obtain the data in each cell of the row
For (short columnindex = 0; columnindex <columncount; columnindex ++ ){
Hssfcell cell = row. getcell (columnindex );

// Obtain the data in the specified Cell
Object cellstr = This. getcellstring (cell );

Rowdata. Add (cellstr );

}

Result. Add (rowdata );
}
}
Return result;
}

/**
* Obtain the content in the cell.
* @ Param Cell
* @ Return
*/
Protected object getcellstring (hssfcell cell ){
Object result = NULL;
If (cell! = NULL ){

Int celltype = cell. getcelltype ();

Switch (celltype ){

Case hssfcell. cell_type_string:
Result = cell. getrichstringcellvalue (). getstring ();
Break;
Case hssfcell. cell_type_numeric:
Result = cell. getnumericcellvalue ();
Break;
Case hssfcell. cell_type_formula:
Result = cell. getnumericcellvalue ();
Break;
Case hssfcell. cell_type_error:
Result = NULL;
Break;
Case hssfcell. cell_type_boolean:
Result = cell. getbooleancellvalue ();
Break;
Case hssfcell. cell_type_blank:
Result = NULL;
Break;
}
}
Return result;
}

Public static void main (string [] ARGs) throws exception {
File file = new file ("D: // TMP // test.xls ");
Excelsheetparser parser = new excelsheetparser (File );
List <list> datas = parser. getdatasinsheet (0 );

For (INT I = 0; I <datas. Size (); I ++) {// display data
List ROW = datas. Get (I );
For (short n = 0; n <row. Size (); N ++ ){
Object value = row. Get (N );
String data = string. valueof (value );
System. Out. Print (Data + "/t ");
}
System. Out. println ();
}
}
}

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.