POI reader EXCEL Implementation Program

Source: Internet
Author: User

The Code is as follows: Copy code

 

/*
* Use POI to read EXCEL files
*/
Import java. io. File;
Import java. io. FileInputStream;
Import java. util. ArrayList;
 
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;
 
/**
*
* @ Author Hanbin
*/
Public class ReadExcel {
 
/**
* @ Param args the command line arguments
*/
Public static void main (String [] args) throws Exception {
Read ("d: demo.xls ");
}

Public static ArrayList read (String fileName ){
ArrayList list = new ArrayList ();
String SQL = "";
Try {
File f = new File (fileName );
FileInputStream FCM = new FileInputStream (f );
HSSFWorkbook wbs = new HSSFWorkbook (FCM );
HSSFSheet childSheet = wbs. getSheetAt (0 );
System. out. println ("number of rows:" + childSheet. getLastRowNum ());
For (int I = 4; I <childSheet. getLastRowNum (); I ++ ){
HSSFRow row = childSheet. getRow (I );
System. out. println ("Number of columns:" + row. getPhysicalNumberOfCells ());
If (null! = Row ){
For (int k = 1; k <row. getPhysicalNumberOfCells (); k ++ ){
HSSFCell cell;
Cell = row. getCell (short) k );
// System. out. print (getStringCellValue (cell) + "t ");
List. add (getStringCellValue (cell) + "t ");
}
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
Return list;
}
/**
* Get the data whose cell data content is string type
*
* @ Param cell: Excel cell
* @ Return String cell data content
*/
Private static String getStringCellValue (HSSFCell cell ){
String strCell = "";
Switch (cell. getCellType ()){
Case HSSFCell. CELL_TYPE_STRING:
StrCell = cell. getStringCellValue ();
Break;
Case HSSFCell. CELL_TYPE_NUMERIC:
StrCell = String. valueOf (cell. getNumericCellValue ());
Break;
Case HSSFCell. CELL_TYPE_BOOLEAN:
StrCell = String. valueOf (cell. getBooleanCellValue ());
Break;
Case HSSFCell. CELL_TYPE_BLANK:
StrCell = "";
Break;
Default:
StrCell = "";
Break;
}
If (strCell. equals ("") | strCell = null ){
Return "";
}
If (cell = null ){
Return "";
}
Return strCell;
}
}


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.