由於項目需求用到excel導入功能,技術有限花了不少時間終於弄出來了。借鑑了別人的方法,分享給有須要的人。html
以前在搜索後看了不少人的方法,可是和我實際的需求不符合。java
借鑑 http://www.cnblogs.com/gmq/archive/0001/01/01/1521496.html 數組
有什麼問題或者改良 能夠聯繫我qq1136533879excel
爲何不符合呢,都是採用遍歷的方法,直接讀取表格內容,不取表頭,固定列數的。
code
File file = new File(path); getData(file, 0); //path是路徑,
正文代碼orm
/** * http://www.cnblogs.com/gmq/archive/0001/01/01/1521496.html * * 讀取Excel的內容,第一維數組存儲的是一行中格列的值,二維數組存儲的是多少個行 循環標題的數組 * * @param file * 讀取數據的源Excel * @param ignoreRows * 讀取數據忽略的行數,比喻行頭不須要讀入 忽略的行數爲1 * @return 讀出的Excel中數據的內容 * @throws FileNotFoundException * @throws IOException */ public String[][] getData(File file, int ignoreRows) throws FileNotFoundException, IOException { List<String[]> result = new ArrayList<String[]>(); int rowSize = 0; BufferedInputStream in = new BufferedInputStream(new FileInputStream( file)); // 打開HSSFWorkbook POIFSFileSystem fs = new POIFSFileSystem(in); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFCell cell = null; String[] bt; // 根據不一樣的data放置不一樣的表頭 Map<Object, Object> headMap = new HashMap<Object, Object>(); for (int sheetIndex = 0; sheetIndex < wb.getNumberOfSheets(); sheetIndex++) { HSSFSheet st = wb.getSheetAt(sheetIndex);// 不取0行 表頭 // 第一行爲標題,不取 for (int rowIndex = ignoreRows; rowIndex <= st.getLastRowNum(); rowIndex++) {// 不算標題 的行數 st.getLastRowNum()不算表頭的數目 HSSFRow row = st.getRow(rowIndex); if (row == null) { continue; } addressList = new AddressList(); boolean _b = false; for (short columnIndex = 0; columnIndex <= row.getLastCellNum(); columnIndex++) { // 循環列 cell = row.getCell(columnIndex); if (cell != null) { String value = cell.toString().trim(); if (rowIndex == 0) { if (value.toString().equals("姓名")) { headMap.put(columnIndex, "name"); }else if (value.toString().equals("職務")) { headMap.put(columnIndex, "zhiwu"); } else if (value.toString().equals("辦公室電話")) { headMap.put(columnIndex, "oph"); } else if (value.toString().equals("手機號碼")) { headMap.put(columnIndex, "tel"); } else if (value.toString().equals("傳真")) { headMap.put(columnIndex, "fax"); } else if (value.toString().equals("郵箱")) { headMap.put(columnIndex, "emall"); } else if (value.toString().equals("生日")) { headMap.put(columnIndex, "birthday"); } else if (value.toString().equals("地址")) { headMap.put(columnIndex, "address"); } else if (value.toString().equals("說明")) { headMap.put(columnIndex, "comments"); } else if (value.toString().equals("性別")) { headMap.put(columnIndex, "sex"); } } else { if (headMap.get(columnIndex) != null) { String _value = headMap.get(columnIndex) .toString(); if (_value.equals("name")) { _b = true; addressList.setName(value); }else if (_value.equals("zhiwu")) { _b = true; String abc = jdbc.GetObject("select id from Code where codeName like '"+value+"'", "id"); addressList.setZhiwu(Integer.parseInt(abc)); } else if (_value.equals("oph")) { _b = true; addressList.setOph(value); } else if (_value.equals("tel")) { _b = true; addressList.setTel(value); } else if (_value.equals("fax")) { _b = true; addressList.setFax(value); } else if (_value.equals("emall")) { _b = true; addressList.setEmall(value); } else if (_value.equals("sex")) { _b = true; if (value.toString().equals("男")) { value = "0"; } else { value = "1"; } addressList.setSex(Integer.parseInt(value)); } else if (_value.equals("birthday")) { _b = true; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = null; try { date = sdf.parse(value); addressList.setBirthday(date); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (_value.equals("address")) { _b = true; addressList.setAddress(value); } else if (headMap.get(columnIndex).toString() .equals("comments")) { _b = true; addressList.setComments(value); } } } } } if (_b) { addressList.setPid(pid); addressList.setType(type); addressList.setUserid(userid); addressList.setSort(0); addressListService.save(addressList); } } } in.close(); String[][] returnArray = new String[result.size()][rowSize]; for (int i = 0; i < returnArray.length; i++) { returnArray[i] = (String[]) result.get(i); } return returnArray; } /** * 去掉字符串右邊的空格 * * @param str * 要處理的字符串 * @return 處理後的字符串 */ public static String rightTrim(String str) { if (str == null) { return ""; } int length = str.length(); for (int i = length - 1; i >= 0; i--) { if (str.charAt(i) != 0x20) { break; } length--; } return str.substring(0, length); }
addressList 是我類。htm
第二種導入方法 這個比較的固定 對象
private List<List<Object>> kydru(String file) throws IOException { List<List<Object>> list = new LinkedList<List<Object>>(); // 構造 XSSFWorkbook 對象,strPath 傳入文件路徑 XSSFWorkbook xwb = new XSSFWorkbook(new FileInputStream(file)); // 讀取第一張表格內容 XSSFSheet sheet = xwb.getSheetAt(0); // Object value = null; XSSFRow row = null; XSSFCell cell = null; for (int i = (sheet.getFirstRowNum() + 1); i <= (sheet .getPhysicalNumberOfRows() - 1); i++) { String value = ""; row = sheet.getRow(i);// row是行號 if (row == null) { continue; } // List<Object> linked = new LinkedList<Object>(); AddressList addressList = new AddressList(); for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) { cell = row.getCell(j); if (cell == null) { // cell是列 1 2 value = ""; } else { DecimalFormat df = new DecimalFormat("0");// 格式化 number // String字符 switch (cell.getCellType()) { case XSSFCell.CELL_TYPE_STRING: value = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_NUMERIC: String shu = df.format(cell.getNumericCellValue()); // 將科學計數的轉換 value = shu; break; case XSSFCell.CELL_TYPE_BOOLEAN: value = cell.getBooleanCellValue() + ","; break; case XSSFCell.CELL_TYPE_BLANK: value = ""; break; default: value = cell.toString(); } } switch (j) { case 0: // 名字爲空就中止循環 addressList.setName(value); break; case 1:// 電話爲空就中止循環 addressList.setTel(value); break; case 2: addressList.setEmall(value); break; case 3: addressList.setFax(value); break; case 4: addressList.setRoomnum(value); break; case 5: addressList.setOph(value); break; case 6: addressList.setAddress(value); break; case 7: addressList.setComments(value); break; case 8: try { addressList.setSort(Integer.parseInt(value)); } catch (Exception e) { continue; // TODO Auto-generated catch block } break; default: break; } String[] val = value.split(","); System.out.println(value); } addressList.setPid(pid); addressList.setUserid(userid); addressList.setType(type); addressList.setSex(0); addressListService.save(addressList); list.add(linked); String[] val2 = value.split(","); System.out.println(val2); } return list; }