概述
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数据
输出结果:
转载于:https://www.cnblogs.com/zanderblogs/p/5543339.html
最后
以上就是过时西牛为你收集整理的JAVA反射机制示例,读取excel数据映射到JAVA对象中的全部内容,希望文章能够帮你解决JAVA反射机制示例,读取excel数据映射到JAVA对象中所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复