Poi enables excel reading and poi for excel reading
Public <T> List <T> readExcel (String path, String sheetName, Integer startRow, T object) throws Exception {
File file = new File (path );
FileInputStream FCM = new FileInputStream (file );
HSSFWorkbook book = new HSSFWorkbook (FCM );
HSSFSheet sheet = book. getSheet (sheetName );
If (sheet = null) return null;
Class <?> ClassType = object. getClass ();
ArrayList <T> objList = new ArrayList <T> ();
Field [] fields = classType. getDeclaredFields (); // obtain the Field in the object.
StartRow --;
For (int I = startRow; I <sheet. getLastRowNum () + 1; I ++ ){
HSSFRow row = sheet. getRow (I );
T objectCopy = (T) classType. getConstructor (new Class [] {}). newInstance (new Object [] {});
For (int j = 0; j <fields. length; j ++ ){
Field field = fields [j];
String fieldName = field. getName ();
Object value = null;
If (field. getType (). equals (String. class )){
If (row. getCell (j )! = Null ){
Row. getCell (j). setCellType (Cell. CELL_TYPE_STRING );
Value = row. getCell (j). getStringCellValue ();
} Else {
Value = "";
}
}
If (field. getType (). equals (java. util. Date. class )){
Value = row. getCell (j). getDateCellValue ();
}
String firstLetter = fieldName. substring (0, 1). toUpperCase ();
String setMethodName = "set" + firstLetter
+ FieldName. substring (1 );
Method setMethod = classType. getMethod (setMethodName,
New Class [] {field. getType ()});
SetMethod. invoke (objectCopy, new Object [] {value });
}
ObjList. add (objectCopy );
}
FCM. close ();
Return objList;
}