POI read the EXCEL implementation program. The code is as follows: Copy the code ** use POI to read the EXCEL file * importjava. io. file; importjava. io. fileInputStream; importjava. util. arrayList; importorg. apache. poi. hssf. usermodel. H
| The code is as follows: |
|
|
/*
* 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
HSSFRow row = childSheet. getRow (I );
System. out. println ("number of columns:" + row. getPhysicalNumberOfCells ());
If (null! = Row ){
For (int k = 1; 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;
}
} |
Notepad/** use POI to read an EXCEL file */import java. io. file; import java. io. fileInputStream; import java. util. arrayList; import org. apache. poi. hssf. usermodel. h...