列字段通用excel导入修改版
生活随笔
收集整理的這篇文章主要介紹了
列字段通用excel导入修改版
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近使用開發的過程中出現了一個小問題,順便記錄一下原因和方法--列字段
入導Excel對網上通用excel入導修改版:處理Excel中單元格為數字問題、可以連續取讀多個sheet ? 每日一道理書,各種各樣的書。書,寄托著人類熱切的希望;書,蘊含著人類豐富的感悟。提起書,會有說不完的話語……
package com.xl.common.excel;import java.io.File; import java.io.FileInputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import com.xl.news.domain.StuInfo;public class ExcelImport { @SuppressWarnings("unchecked")public Collection importExcel(File file ,Class obj,String... pattern) { Collection dist = new ArrayList(); try { Field filed[] = obj.getDeclaredFields(); // 失掉目標目標類的有所的字段表列 // 將有所標有Annotation的字段,也就是答應入導據數的字段,放入到一個map中 Map<String,Method> fieldSetMap = new HashMap<String,Method>(); Map<String,Method> fieldSetConvertMap = new HashMap<String,Method>(); // 環循取讀有所字段 for (int i = 0; i < filed.length; i++) { Field f = filed[i]; // 失掉單個字段上的Annotation ExcelAnnotation excel = f.getAnnotation(ExcelAnnotation.class);// 如果標識了Annotationd的話 if (excel != null) { // 結構置設了Annotation的字段的Setter方法 String fieldname = f.getName(); String setMethodName = "set" + fieldname.substring(0, 1).toUpperCase() + fieldname.substring(1); // 結構調用的method, Method setMethod = obj.getMethod(setMethodName, new Class[] { f.getType() }); // 將這個method以Annotaion的名字為key來存入。 // 對于重名將致導 覆蓋失敗,對于此處的制約須要 fieldSetMap.put(excel.exportName(), setMethod); if(excel.importConvertSign()==1) { StringBuffer setConvertMethodName = new StringBuffer("set"); setConvertMethodName.append(fieldname.substring(0, 1) .toUpperCase()); setConvertMethodName.append(fieldname.substring(1)); setConvertMethodName.append("Convert"); Method getConvertMethod = obj.getMethod(setConvertMethodName.toString(),new Class[] {String.class}); fieldSetConvertMap.put(excel.exportName(), getConvertMethod); } } } FileInputStream in = new FileInputStream(file); // 將傳入的File結構為FileInputStream; HSSFWorkbook book = new HSSFWorkbook(in); // 失掉工作表 in.close();// 關閉流for (int j = 0; j < book.getNumberOfSheets() - 1; j++ ) // book.getNumberOfSheets() - 1 取獲工作表中頁數{HSSFSheet sheet = book.getSheetAt(j); // 失掉第j頁 Iterator<Row> row = sheet.rowIterator(); // 失掉第j頁的有所行 //System.out.println(row.hasNext());while (row.hasNext()){ //判斷前當sheet否是為空Row title = row.next(); // 失掉第一行,也就是標題行 Iterator<Cell> cellTitle = title.cellIterator(); // 失掉第一行的有所列 Map titlemap = new HashMap(); // 將標題的文字內容放入到一個map中。 int i = 0; // 從標題第一列開始 // 環循標題有所的列 while (cellTitle.hasNext()) { Cell cell = cellTitle.next(); String value = cell.getStringCellValue(); titlemap.put(i, value); i = i + 1; } //用來格式化日期的DateFormat // SimpleDateFormat sf; // if(pattern.length<1) // { // sf=new SimpleDateFormat("yyyy-MM-dd"); // } // else { // sf=new SimpleDateFormat(pattern[0]); // } while (row.hasNext()) { Row rown = row.next();// 標題下的第一行 Iterator<Cell> cellbody = rown.cellIterator();// 行的有所列 Object tObject = obj.newInstance(); // 失掉傳入類的例實 int k = 0; // 歷遍一行的列 while (cellbody.hasNext()) { Cell cell = cellbody.next(); String titleString = (String) titlemap.get(k); // 這里失掉此列的對應的標題 // 如果這一列的標題和類中的某一列的Annotation雷同,那么則調用此類的的set方法,行進設值 if (fieldSetMap.containsKey(titleString)) { Method setMethod = (Method) fieldSetMap.get(titleString); Type[] ts = setMethod.getGenericParameterTypes(); // 失掉setter方法的參數 String xclass = ts[0].toString(); // 只要一個參數 //System.out.println("類型: "+xclass); // 判斷參數類型 if (fieldSetConvertMap.containsKey(titleString)) { fieldSetConvertMap.get(titleString).invoke(tObject, cell.getStringCellValue()); } else { if (xclass.equals("class java.lang.String")) { if(cell.getCellType()==cell.CELL_TYPE_NUMERIC ){cell.setCellType(Cell.CELL_TYPE_STRING);setMethod.invoke(tObject, cell.getStringCellValue());}else{setMethod.invoke(tObject, cell.getStringCellValue()); }} else if (xclass.equals("class java.util.Date")) { setMethod.invoke(tObject, cell.getDateCellValue()); } else if (xclass.equals("class java.lang.Boolean")) { setMethod.invoke(tObject, cell.getBooleanCellValue()); } else if (xclass.equals("class java.lang.Integer")) { setMethod.invoke(tObject, new Integer(cell.getStringCellValue())); }else if(xclass. equals("class java.lang.Long")) { setMethod.invoke(tObject,new Long( cell.getStringCellValue())); } } } k = k + 1;// 下一列 } dist.add(tObject); } }} } catch (Exception e) { e.printStackTrace(); return null; } return dist; } @SuppressWarnings("unchecked")public static void main(String[] args) { ExcelImport test = new ExcelImport(); File file = new File("D://testOne.xls"); Long befor = System.currentTimeMillis(); List<StuInfo> result = (ArrayList) test.importExcel(file,StuInfo.class); Long after = System.currentTimeMillis(); System.out.println("此次作操共耗時:" + (after - befor) + "毫秒"); for (int i = 0; i < result.size(); i++) { StuInfo testpojo=result.get(i); System.out.println("入導的信息為:"+testpojo.getStuName()+ "--"+testpojo.getStuID()+"-"+testpojo.getClassID()+"--"+testpojo.getIdentificationID()); } System.out.println("共轉化為List的行數為:" + result.size()); } }
文章結束給大家分享下程序員的一些笑話語錄: 手機終究會變成PC,所以ip會比wm更加暢銷,但是有一天手機強大到一定程度了就會發現只有wm的支持才能完美享受。就好比樹和草,草長得再高也是草,時間到了條件成熟了樹就會竄天高了。www.ishuo.cn
轉載于:https://www.cnblogs.com/xinyuyuanm/archive/2013/04/29/3050770.html
總結
以上是生活随笔為你收集整理的列字段通用excel导入修改版的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝桥杯比武问题
- 下一篇: 戴尔笔记本win8全新安装