浏览 1850 次
锁定老帖子 主题:java 导入Excel 为 List
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2011-10-17
用户需求中曾经有写需要把Excel中的数据导入数据库中,不过每次都要一一对应Excel中的数据然后写程序,太繁琐, 后来想了一下,改成这样的,以后就方便导入数据,直接遍历List就可以把数据导入进来了。 /** * * @param filePath 文件路径 * @param clazz 存放的数据是那个对象的数据:类似User.class * @param field 标题中那个标题对应那个对象中的那个字段:map.add("Excel标题", "user的字段值") * @return */ private static List getExcel(String filePath, Class clazz, Map<String, String> field) { if (!filePath.endsWith("xls")) { System.out.println("文件不是Excel"); return null; } List list = new ArrayList(); Workbook wk = null; try { wk = Workbook.getWorkbook(new File(filePath)); Sheet sheet = wk.getSheet(0); Map<Integer, String> columnMap = readTitle(field, sheet.getRow(0)); if (columnMap == null || columnMap.size() == 0) { System.out.println("没有查到相应的标题"); return null; } for (int i = 1; i < sheet.getRows(); i++) { Cell[] cells = sheet.getRow(i); if (cells.length == 0) continue; Set<Map.Entry<Integer, String>> set = columnMap.entrySet(); Object obj = clazz.newInstance(); for (Iterator<Map.Entry<Integer, String>> iter = set.iterator(); iter .hasNext();) { Map.Entry<Integer, String> entry = iter.next(); String fieldValue = cells[entry.getKey()].getContents() .trim(); String fieldName = entry.getValue(); invokeMethod(clazz, obj, fieldName, fieldValue); } list.add(obj); } } catch (Exception e) { e.printStackTrace(); return null; } finally { if (wk != null) wk.close(); } return list; } private static Object invokeMethod(Class<?> clazz, Object instance, String fileName, String fieldValue) throws Exception { Method getMethod = clazz.getMethod("get" + toFirstUpper(fileName)); Class<?> paraType = getMethod.getReturnType(); Method setMethod = clazz.getMethod("set" + toFirstUpper(fileName), paraType); Object value = transfType(paraType, fieldValue); if (value != null) setMethod.invoke(instance, value); return instance; } private static Object transfType(Class<?> paraType, String target) { Object result = null; if (paraType == String.class) { result = target; } else if (paraType == Double.class) { result = new Double(Double.parseDouble(target)); } else if (paraType == double.class) { result = Double.parseDouble(target); } else if (paraType == boolean.class) { result = Boolean.parseBoolean(target); } else if (paraType == Boolean.class) { result = Boolean.valueOf(target); } else if (paraType == int.class) { result = Integer.parseInt(target); } else if (paraType == Integer.class) { result = Integer.decode(target); } else if (paraType == long.class) { result = Long.parseLong(target); } else if (paraType == Long.class) { result = Long.decode(target); } else if (paraType == float.class) { result = Float.parseFloat(target); } else if (paraType == Float.class) { result = Float.valueOf(target); } return result; } public static String toFirstUpper(String str) { return str.replaceFirst(str.substring(0, 1), str.substring(0, 1) .toUpperCase()); } private static Map<Integer, String> readTitle(Map<String, String> fieldMap, Cell[] cells) { Map<Integer, String> columnMap = new HashMap<Integer, String>(); for (int i = 0; i < cells.length; i++) { String content = cells[i].getContents().trim(); String field = fieldMap.get(content); if (field != null) { columnMap.put(i, field); } } return columnMap; } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |