本文是使用 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; }