由於基於Oracle數據庫上做開發,因此常常會需要把大量的Excel數據導入到Oracle數據庫中,其實如果從事SqlServer數據庫的開發,那么思路也是一樣的,本文主要介紹如何導入Excel數據進入Oracle數據庫的內容。 方法一: 1.准備數據:在excel中構造出需要的數據 2.將excel中的數據另存為文本文件(有制表符分隔的) 3.將新保存到文本文件中的數據導入到pl*sql中 在pl*sql中選擇tools--text importer,在出現的窗口中選擇Data from Textfile,然后再選擇Open data file, 在彈出的文件選擇框中選中保存有數據的文本文件,此時將會看到data from textfile中顯示將要導入的數據 4.在configuration中進行如下配置 注:如果不將Name in header勾選上會導致字段名也當做記錄被導入到數據庫中,從而導致數據錯誤 5.點擊data to oracle,選擇將要導入數據的表,並在fields中將文本中的字段與表中的字段進行關聯 6.點擊import按鈕進行導入 7.查看導入的數據 OK,至此數據導入成功。
方法二: String fileName = "F:\\xx.xls"; //"F:\\document/test/aa.xls"; public static void createBusinessFinish(String fileName) { try { //申辦 EntitySet<laam_ex_sb> sbSet = getExcelSBdata(fileName); List<laam_ex_sb> sblist = null; if (sbSet != null) { sblist = sbSet.getResult(); for (int i = 0; i < sblist.size(); i++) { laam_ex_sb sb = sblist.get(i); sb.setID(Global.getInstance().GetUUID()); String sbStr = Global.getInstance().getSerializService().Serialize(sb); EntityBean bean = (EntityBean) Global.getInstance().getSerializService().DeSerialize(sbStr, EntityBean.class); bean.setbeanname("laam_ex_sb"); bean.insert(); } } } public static EntitySet<laam_ex_sb> getExcelSBdata (String fileName) { try { //直接從本地文件創建Workbook FileInputStream instream = new FileInputStream(File.get(fileName)); HSSFWorkbook hssfworkbook = new HSSFWorkbook(instream); HSSFSheet hssfsheet = hssfworkbook.getSheetAt(0);//第一個工作表 //Map<String, Object> map = new HashMap<String, Object>(); EntitySet<laam_ex_sb> beanSet = new EntitySet<laam_ex_sb>(); String busino = null; String sblsh = null; String sbwd = null; //遍歷該行所有的行,j表示行數, getPhysicalNumberOfRows行的總數 ArrayList<laam_ex_sb> list = new ArrayList<laam_ex_sb>();//創建List 集合 laam_ex_sb entity = null; Row row = null; int rows = hssfsheet.getPhysicalNumberOfRows(); //總行數 for (int j = 1; j < rows; j++) { entity = new laam_ex_sb(); row = hssfsheet.getRow(j); //獲取行數據對象(0是表頭) if ( row == null ) { System.out.println("提示:\n" + (j + 1) + " 行沒有數據。\n"); break; } /** */ /**將EXCEL中的第 j 行,第一列的值插入到實例中*/ //獲取每一行的列 int k = 0; //事項名稱 if ( row.getCell(k) == null ) { entity.setSxmc(""); } else { entity.setSxmc(row.getCell(k).getStringCellValue().trim()); if ( j == 1 ) { sxmc = row.getCell(k).getStringCellValue().trim(); } } k++; //申請人類型 if ( row.getCell(k) == null ) { entity.setSqrlx("1"); } else { try { entity.setSqrlx(Math.round(row.getCell(k).getNumericCellValue()) + ""); } catch (Exception e) { entity.setSqrlx(row.getCell(k).getStringCellValue().trim()); } } k++; //申請人名稱 if ( row.getCell(k) == null ) { entity.setSqrmc(""); } else { try { entity.setSqrmc(row.getCell(k).getStringCellValue().trim()); } catch (Exception e) { entity.setSqrmc(Math.round(row.getCell(k).getNumericCellValue()) + ""); } } k++; //申請人證件號碼 if ( row.getCell(k) == null ) { entity.setSqrzjhm(""); } else { String card = null; try { card = row.getCell(k).getStringCellValue().trim(); } catch (Exception e) { card = Math.round(row.getCell(k).getNumericCellValue()) + ""; } finally { String c = new NSgetProcessData().getRandomID(); if ( card == null || "".equals(card) || card.length() < 15 ) { card = c;//得到一個隨機的身份證號碼 } entity.setSqrzjhm(card); } } k++; //聯系人姓名 if ( row.getCell(k) == null ) { entity.setLxrxm(""); } else { try { entity.setLxrxm(row.getCell(k).getStringCellValue().trim()); } catch (Exception e) { entity.setLxrxm(Math.round(row.getCell(k).getNumericCellValue()) + ""); } } k++; //聯系人手機 if ( row.getCell(k) == null ) { entity.setLxrsj(new NSgetProcessData().getPhone()); } else { String phone = null; try { phone = Math.round(row.getCell(k).getNumericCellValue()) + ""; } catch (Exception e) { phone = row.getCell(k).getStringCellValue().trim(); }finally{ if ( phone == null || "".equals(phone)|| phone.length()<8|| phone.length()>11) { phone = new NSgetProcessData().getPhone();//得到一個隨機的手機號碼 } entity.setLxrsj(phone); } } k++; //申辦項目名稱 if ( row.getCell(k) == null ) { entity.setSbxmmc(""); } else { try { entity.setSbxmmc(row.getCell(k).getStringCellValue().trim()); } catch (Exception e) { entity.setSbxmmc(Math.round(row.getCell(k).getNumericCellValue()) + ""); } } k++; //申辦材料清單 if ( row.getCell(k) == null ) { entity.setSbclqd("無需提交材料"); } else { try { entity.setSbclqd(row.getCell(k).getStringCellValue().trim()); } catch (Exception e) { entity.setSbclqd(Math.round(row.getCell(k).getNumericCellValue()) + ""); } } k++; //提交方式 if ( row.getCell(k) == null ) { entity.setTjfs("1"); } else { try { entity.setTjfs(Math.round(row.getCell(k).getNumericCellValue()) + ""); } catch (Exception e) { entity.setTjfs(row.getCell(k).getStringCellValue().trim()); } } k++; //申辦時間 if ( row.getCell(k) == null ) { entity.setSbsj("2014-07-05 09:30:24"); } else { String year = row.getCell(k).getDateCellValue().getYear() + 1900 + ""; int mon = row.getCell(k).getDateCellValue().getMonth() + 1; String month = mon < 10 ? "0" + mon : mon + ""; int d = row.getCell(k).getDateCellValue().getDate(); String day = d < 10 ? "0" + d : d + ""; Random rd = new Random(); String hour = ""; if (sxmc.contains("社會投資項目備案") || sxmc.contains("接收高校應屆畢業生")) { //當為即辦件的時候 String[] number = { "09", "10"}; hour = number[rd.nextInt(number.length)]; }else { String[] number = { "09", "10", "11", "12", "13", "14", "15", "16", "17" }; hour = number[rd.nextInt(number.length)]; } int m = rd.nextInt(60); String minite = m < 10 ? "0" + m : m + ""; int s = rd.nextInt(60); String secend = s < 10 ? "0" + s : s + ""; String timer =year + "-" + month + "-" + day + " " + hour + ":" + minite + ":" + secend; entity.setSbsj(timer); } if ( j == 1 ) { EntityBean result = Global.getInstance().getDataSource().beanExecuteFirstRow("select busino,businame,"+ "(select dirname from laambusinessdir where id=laambusiness.dirid)||'網上窗口' deptname from laambusiness where businame='" + sxmc + "'"); busino = result.getString("busino"); sbwd = result.getString("deptname"); //busiService bService = new busiService(); } String prefix = busino.substring(0, 19); sblsh = GlobalSNService.getSN("lgbsShenbanLiushuiHao", "{date:yyMMdd}{sn:xxxx}", prefix==null?"A":prefix, null, 1, null, new Date()); entity.setSblsh(sblsh); //申辦流水號 entity.setSxbm(busino); //事項編碼 entity.setSbjtwd(sbwd); //申辦具體網點 entity.setXzqhdm("440305"); //業務發生所在地行政區划代碼 entity.setSbhzh(sblsh); //申辦回執號 list.add(entity); } int z = hssfsheet.getPhysicalNumberOfRows(); int nextLine = list.size() + 1; if ( list.size() < z - 1 ) { System.out.println("導入提示:\n" + "成功導入" + list.size() + "條數據,請根據提示檢查第:" + nextLine + "行數據"); } else { System.out.println("導入提示:\n" + "成功導入" + list.size() + "條數據"); } beanSet.setResult(list); return beanSet; } catch (Exception e) { Global.getInstance().LogError(e); return null; } }