您好,登錄后才能下訂單哦!
本文是使用 org.apache.poi 進(jìn)行一次簡(jiǎn)單的封裝,適用于大部分 excel 導(dǎo)入導(dǎo)出功能。過程中可能會(huì)用到反射,如若有對(duì)于性能有極致強(qiáng)迫癥的同學(xué),看看就好。
由于 poi 本身只是針對(duì)于 excel 等office軟件的一個(gè)工具包,在一些常規(guī)的 excel 導(dǎo)入導(dǎo)出時(shí),還需要再做一次精簡(jiǎn)的封裝,簡(jiǎn)化代碼耦合。
本人經(jīng)歷過幾家公司的代碼封裝,導(dǎo)入導(dǎo)出一般存在下面的情況。
總結(jié):如果只有上述的選擇,本人是比較傾向于第二種,畢竟對(duì)外層是非常友好的
總結(jié):如果只有上述的選擇,本人是比較傾向于第三種,第三種只遍歷一次,并且外部未做處理。但是按第四種模式來看,那么第三種模式還是會(huì)存在日期格式問題,這個(gè)我們后續(xù)再分析如何處理。
/**
* excel導(dǎo)入
* @param keys 字段名稱數(shù)組,如 ["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; // 為空時(shí),下一列
// 根據(jù)poi返回的類型,做相應(yīng)的get處理
if(Cell.CELL_TYPE_STRING == cell.getCellType()) {
value = cell.getStringCellValue();
} else if(Cell.CELL_TYPE_NUMERIC == cell.getCellType()) {
value = cell.getNumericCellValue();
// 由于日期類型格式也被認(rèn)為是數(shù)值型,此處判斷是否是日期的格式,若時(shí),則讀取為日期類型
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導(dǎo)出
* @param fileNamePath 導(dǎo)出的文件名稱
* @param sheetName 導(dǎo)出的sheet名稱
* @param list 數(shù)據(jù)集合
* @param titles 第一行表頭
* @param fieldNames 字段名稱數(shù)組
* @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 {}
// 遍歷生成數(shù)據(jù)行,通過反射獲取字段的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) { // 不存在該方法,查看父類是否存在。此處只支持一級(jí)父類,若想支持更多,建議使用while循環(huán)
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("第三方手動(dòng)閥");
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; // 擴(kuò)展字段
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public String getCreateTimeStr() {
createTimeStr = DateUtil.formatDatetime(this.createTime);
return createTimeStr;
}
GJP-Example-ExcelUtil 代碼下載
blog.guijianpan.com
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。