asp實現excel中的資料匯入資料庫_應用技巧

來源:互聯網
上載者:User

asp實現excel中的資料匯入資料庫

<% Response.CodePage=65001%><% Response.Charset="UTF-8" %><%wenjian = request.Form("select") '擷取副檔名ext = FileExec(wenjian)'判斷副檔名if ext <> "xls" then  response.Write("<script>alert('檔案類型不對,請核實!');window.location.href='index.html';</script>")  response.End()end if Dim objConn,objRSDim strConn,strSql set objConn=Server.CreateObject("ADODB.Connection")set objRS=Server.CreateObject("ADODB.Recordset") excelFile = server.mappath(wenjian) '針對excel 2007strConn = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & excelFile & ";" & "Extended Properties=Excel 8.0;"objConn.Open strConn strSql="SELECT * FROM [Sheet1$]" objRS.Open strSql,objConn,1,1objRS.MoveFirst %><!--#include file="conn.asp"--><%'迴圈excel中所有記錄while not objRS.eof   set rs = Server.CreateObject("Adodb.Recordset")  '查詢語句  sql_s = "select * from ceshi where lname='" & objRS(0) & "' and old='" & objRS(1) & "' and sex='" & objRS(2) & "' and guojia='" & objRS(3) & "' and QQ='" & objRS(4) & "'"  rs.open sql_s, conn, 1, 1  '重複的資料不做錄入操作  if rs.eof then    '插入語句    '****excel中第一條不會被錄入****    sql = "insert into ceshi (lname, old, sex, guojia, QQ)values ('" & objRS(0) & "', '" & objRS(1) & "', '" & objRS(2) & "', '" & objRS(3) & "', '" & objRS(4) & "')"    '執行插入    conn.execute(sql)  end if  objRS.MoveNext   rs.close  set rs = nothingwend '又到了各種關閉的時候conn.closeset conn = nothingobjRS.CloseobjConn.Closeset objRS = Nothingset objConn = Nothing response.Write("<script>alert('匯入成功');window.location.href='index.html';</script>")response.End() Function FileExec(fileName) FileExec = Mid(fileName,Instr(fileName,".")+1,Len(fileName)-Instr(fileName,"."))End Function%>

再分享一個簡化版的代碼

wenjian=request.Form("floor")fileext=mid(wenjian,InStrRev(wenjian,".")+1)if lcase(fileext)<>"xls" thenresponse.write "<script>alert ('檔案格式不對,請上傳Excel檔案');window.location.href='updateFloor.asp';</script>"response.endend ifset conne=server.CreateObject("ADODB.Connection")connStre="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath( ""&wenjian&"" )&";Extended Properties='Excel 8.0;HDR=YES;IMEX=1';"conne.open connStreSqle="select * from [sheet1$] " Set rse = Server.CreateObject("ADODB.Recordset")rse.open sqle,conne,1,1'驗證hang=2do while not rse.eof'名稱不可為空if trim(rse(0))<>"" thenelsemess="第"& hang &"行名稱為空白,請檢查!"response.Write"<script>alert('"& mess &"').window.location.href='updateFloor.asp'</script>"response.End()end if rse.movenexthang=hang+1looprse.movefirstdo while not rse.eofset rst=server.CreateObject("adodb.recordset")sqlt="select * from Sellman"rst.open sqlt,conn,1,3rst.addnew()rst("CompanyName")=c2(rse(0))rst("CompanyInfo")=c2(rse(1))rst("address")=c2(rse(2))rst("tel")=c2(rse(3))&"  "&c2(rse(7))rst("Fax")=c2(rse(4))rst("linkman")=c2(rse(5))rst("Homepage")=c2(rse(8))rst("Email")=c2(rse(6))rst.update()rst.closeset rst=nothingrse.movenextlooprse.closeset rse=nothingresponse.Write "<script>alert('匯入成功!');location.href='updateFloor.asp';</script>"

其實簡單的說象access 資料庫一樣,把excel檔案開啟,再進行讀再寫到access中你要寫到sqlserver中就把寫的過程改一下就成了

看下代碼:

dim conn dim conn2 set conn=CreateObject("ADODB.Connection") conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Data Source=c:\book1.mdb" set conn2=CreateObject("ADODB.Connection") conn2.Open "Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=;Extended properties=Excel 5.0;Data Source=c:\book1.xls" sql = "SELECT * FROM [Sheet1$]" set rs = conn2.execute(sql) while not rs.eof sql = "insert into xxx([a],[b],[c],[d]) values('"& fixsql(rs(0)) &"','"& fixsql(rs(1)) &"','"& fixsql(rs(2)) &"','"& fixsql(rs(3)) &"')" conn.execute(sql) rs.movenext wend conn.close set conn = nothing conn2.close set conn2 = nothing function fixsql(str) dim newstr newstr = str if isnull(newstr) then newstr = "" else newstr = replace(newstr,"'","''") end if fixsql = newstr end function 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.