JAVA反射机制示例,读取excel数据映射到JAVA对象中

时间:2023-03-08 15:54:51
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet; /**
* 读取excel数据映射到指定bean中
* @author
* May 30, 2016 5:38:33 PM
*/
public class MyExcelReader {
/**
*
* @author
* May 30, 2016 5:39:01 PM
* @param beginRow
* @param beanpros
* @param classPathName
* @param results
* @param sheet
* @return
*/
public String getDatasByrc(int beginRow,Map<Integer,String> beanpros,String classPathName,List<Object> results,Sheet sheet){
try {
Class clazz = Class.forName(classPathName);
Set<Integer> set = beanpros.keySet() ;
Row row = sheet.getRow(beginRow);
while(row!=null) {
Object obj = clazz.newInstance() ;
for(Integer key:set){
String value = "" ;
if(row!=null){
Cell cell = row.getCell(key) ;
if(cell!=null){
int type = cell.getCellType() ;
if(type == Cell.CELL_TYPE_STRING){
value = cell.getStringCellValue() ;
}else if(type==Cell.CELL_TYPE_NUMERIC||type==Cell.CELL_TYPE_FORMULA){
value = String.valueOf(cell.getNumericCellValue());
}else if(type==Cell.CELL_TYPE_BOOLEAN){
value = String.valueOf(cell.getBooleanCellValue()) ;
}
}
}
PropertyDescriptor pd = new PropertyDescriptor(beanpros.get(key),clazz);
Method wM = pd.getWriteMethod();
wM.invoke(obj, value);
}
results.add(obj);
row = sheet.getRow(++beginRow);
}
} catch (Exception e) {
e.printStackTrace();
return "用于接受结果的bean不存在";
} return null ;
} public static void main(String[] args) {
MyExcelReader mer = new MyExcelReader() ;
try {
HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File("D:/testimport.xls")));
Sheet sheet = wb.getSheetAt(0);
List<Object> results = new ArrayList<Object>() ;
Map<Integer,String> beanpros = new HashMap<Integer, String>() ;
beanpros.put(0, "username");
beanpros.put(1, "password");
String s = mer.getDatasByrc(0, beanpros, "SysUser", results, sheet);
if(s==null){
for(Object obj:results){
SysUser bean = (SysUser) obj ;
System.out.println(bean.getUsername()+"---"+bean.getPassword());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class SysUser {

    private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

Excel数据

JAVA反射机制示例,读取excel数据映射到JAVA对象中

输出结果:

JAVA反射机制示例,读取excel数据映射到JAVA对象中