本文是使用 org.apache.poi 进行一次简单的封装,适用于大部分 excel 导入导出功能。过程当中可能会用到反射,如如有对于性能有极致强迫症的同窗,看看就好。java
因为 poi 自己只是针对于 excel 等office软件的一个工具包,在一些常规的 excel 导入导出时,还须要再作一次精简的封装,简化代码耦合。apache
本人经历过几家公司的代码封装,导入导出通常存在下面的状况。json
总结:若是只有上述的选择,本人是比较倾向于第二种,毕竟对外层是很是友好的api
总结:若是只有上述的选择,本人是比较倾向于第三种,第三种只遍历一次,而且外部未作处理。可是按第四种模式来看,那么第三种模式仍是会存在日期格式问题,这个咱们后续再分析如何处理。数组
/** * excel导入 * @param keys 字段名称数组,如 ["id", "name", ... ] * @param filePath 文件物理地址 * @return * @author yzChen * @date 2016年12月18日 下午2:46:51 */ public static List<Map<String, Object>> imp(String filePath, String[] keys) throws Exception {}
// 遍历该行全部列 for (short j = 0; j < cols; j++) { cell = row.getCell(j); if(null == cell) continue; // 为空时,下一列 // 根据poi返回的类型,作相应的get处理 if(Cell.CELL_TYPE_STRING == cell.getCellType()) { value = cell.getStringCellValue(); } else if(Cell.CELL_TYPE_NUMERIC == cell.getCellType()) { value = cell.getNumericCellValue(); // 因为日期类型格式也被认为是数值型,此处判断是不是日期的格式,若时,则读取为日期类型 if(cell.getCellStyle().getDataFormat() > 0) { value = cell.getDateCellValue(); } } else if(Cell.CELL_TYPE_BOOLEAN == cell.getCellType()) { value = cell.getBooleanCellValue(); } else if(Cell.CELL_TYPE_BLANK == cell.getCellType()) { value = cell.getDateCellValue(); } else { throw new Exception("At row: %s, col: %s, can not discriminate type!"); } map.put(keys[j], value); }
String filePath = "E:/order.xls"; String[] keys = new String[]{"id","brand"}; List<Map<String, Object>> impList; try { impList = ExcelUtil.imp(filePath, keys); for (Map<String, Object> map : impList) { System.out.println(map.get("brand")); } } catch (Exception e) { e.printStackTrace(); }
/** * excel导出 * @param fileNamePath 导出的文件名称 * @param sheetName 导出的sheet名称 * @param list 数据集合 * @param titles 第一行表头 * @param fieldNames 字段名称数组 * @return * @throws Exception * @author yzChen * @date 2017年5月6日 下午3:53:47 */ public static <T> File export(String fileNamePath, String sheetName, List<T> list, String[] titles, String[] fieldNames) throws Exception {}
// 遍历生成数据行,经过反射获取字段的get方法 for (int i = 0; i < list.size(); i++) { t = list.get(i); HSSFRow row = sheet.createRow(i+1); Class<? extends Object> clazz = t.getClass(); for(int j = 0; j < fieldNames.length; j++){ methodName = "get" + capitalize(fieldNames[j]); try { method = clazz.getDeclaredMethod(methodName); } catch (java.lang.NoSuchMethodException e) { // 不存在该方法,查看父类是否存在。此处只支持一级父类,若想支持更多,建议使用while循环 if(null != clazz.getSuperclass()) { method = clazz.getSuperclass().getDeclaredMethod(methodName); } } if(null == method) { throw new Exception(clazz.getName() + " don't have menthod --> " + methodName); } ret = null == method.invoke(t) ? null : method.invoke(t) + ""; setCellGBKValue(row.createCell(j), ret + ""); } }
String[] titles = new String[]{"Id", "Brand"}; String[] fieldNames = new String[]{"id", "brand"}; List<Order> expList = new ArrayList<Order>(); Order order = new Order(); order.setId(1L); order.setBrand("第三方手动阀"); expList.add(order); order = new Order(); order.setId(2L); order.setBrand("scsdsad"); expList.add(order); String fileNamePath = "E:/order.xls"; try { ExcelUtil.export(fileNamePath, "订单", expList, titles, fieldNames); } catch (Exception e) { e.printStackTrace(); }
private Date createTime; private String createTimeStr; // 扩展字段 public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public String getCreateTimeStr() { createTimeStr = DateUtil.formatDatetime(this.createTime); return createTimeStr; }