excel導入sql數據庫方法
導讀:在網上看到很多朋友在咨詢excel導入sql數據庫方法,那么今天就給大家分享一下實現的方法。先介紹一個思路:將excel文件上傳到服務器,然后預覽數據,可以對數據進行篩選,點擊“導入”后將選擇的數據導入到sql數據庫中,最后把上傳的文件從服務器上刪除,釋放空間。好了,接下來我們就按步驟來講解excel導入sql數據庫方法。
1、excel文件上傳到服務器
我們采用函數的實現,代碼如下:
call UploadFile(fName,FilePath)
Function UploadFile(fName,FilePath)
UpLoadFileType="xls|" //設置允許上傳的文件類型
FileTypeErr=True
set upload=new upload_file
for each formName in upload.File
set file=upload.File(formName)
fName=file.FileName
FileExt=file.FileExt
if file.filesize<100 then
Response.Write "<script>"
Response.Write "alert('文件地址不能為空');"
Response.Write "location.href= 'Request.asp';"
Response.Write "</script>"
Response.end()
end if
UpLoadFile_ID=split(UpLoadFileType,"|")
for i=0 to ubound(UpLoadFile_ID)
if UpLoadFile_ID(i)=FileExt then
FileTypeErr=False
exit for
end if
next
if FileTypeErr=True then
Response.Write "<script>"
Response.Write "alert('文件類型錯誤');"
Response.Write "location.href= 'Request.asp';"
Response.Write "</script>"
Response.end()
end if
mapurl=trim(fName)
file.SaveToFile Server.mappath(mapurl) //保存文件
set file=nothing
next
set upload=nothing
FilePath=Server.mappath(mapurl) //將上傳的文件路徑賦值給FilePath
End Function
2、預覽數據,選擇數據導入到sql數據庫中
我們采用函數的實現,代碼如下:
call ExcelToSql(fName,FilePath)
Function ExcelToSql(ExName,FilePath,Mainbody)
Dim conn_xls
Dim StrConn_xls
Dim Rs
Dim Sql
Dim i
ExTName = "Sheet1"
On Error Resume Next
Set conn_xls =Server.CreateObject("ADODB.Connection")
StrConn_xls="Driver={Microsoft Excel Driver (*.xls)};DBQ="& Server.MapPath(""&ExName)
conn_xls.Open StrConn_xls
Set rs_xls = Server.CreateObject("ADODB.Recordset")
Sql="select * from ["&ExTName&"$]"
rs_xls.Open Sql,conn_xls ,3,3
If Err Then
err.Clear
Set conn_xls = Nothing
set rs_xls=nothing
Delete_UpLoadFile(FilePath)
Response.Write "<script>"
Response.Write "alert('文件格式有誤');"
Response.Write "location.href= 'Request.asp';"
Response.Write "</script>"
Response.end()
End If
if rs_xls.eof then
Set conn_xls = Nothing
set rs_xls=nothing
Delete_UpLoadFile(FilePath)
Response.Write "<script>"
Response.Write "alert('Excel文件中無數據');"
Response.Write "location.href= 'Request.asp';"
Response.Write "</script>"
Response.end()
else
i=1
mainbody=mainbody & "<table width=""100%"" border=""0"" cellspacing=""1"" cellpadding=""0"" class=""border"">"
mainbody=mainbody & "<tr class=""title""><td height=""20"" width=""10%"" align=""center""> </td><td height=""20"" width=""30%"" align=""center"">編號
</td><td width=""20%"" align=""center"">姓名</td><td width=""20%"" align=""center"">性別</td><td width=""20%"" align=""center"">省份</td></tr>"
do while not rs_xls.eof
'由于excel在處理數據類型時,前面無法帶0,補0操作
if len(Rs_xls(0))<10 then
cc=""
aa=10-len(Rs_xls(0))
for c=1 to aa
cc=cc & "0"
next
end if
payrollcode=cc & Rs_xls(0)
'如果導入的excel數據列有變化,請對程序做適當的修改
mainbody=mainbody & "<tr class=""tdbg"" onMouseOut=""this.className='tdbg'"" onMouseOver=""this.className='tdbg2'"">"
mainbody=mainbody & "<td height=""20"" align=""center""><input type=""checkbox"" name=""ID"&i&""" value=""ON"" checked></td>"
mainbody=mainbody & "<td align=""center"">"&payrollcode&"<input type=""hidden"" name=""payrollcode"&i&""" value="&payrollcode&"></td>"
mainbody=mainbody & "<td align=""center"">"&Rs_xls(1)&"<input type=""hidden"" name=""cname"&i&""" value="""&trim(Rs_xls(1))&"""></td>"
mainbody=mainbody & "<td align=""center"">"&Rs_xls(2)&"<input type=""hidden"" name=""***"&i&""" value="""&trim(Rs_xls(2))&"""></td>"
mainbody=mainbody & "<td align=""center"">"&Rs_xls(3)&"<input type=""hidden"" name=""shengfen"&i&""" value="""&trim(Rs_xls(3))&"""></td>"
mainbody=mainbody & "</tr>"
i = i +1
rs_xls.Movenext
Loop
end if
rs_xls.close
set rs_xls=nothing
conn_xls.close
set StrConn_xls=nothing
mainbody=mainbody & "<tr class=""tdbg""><td height=""20"" colspan=""10""> 總數:"&i-1&"</td></tr>"
mainbody=mainbody & "</table><p align=""center""><input type=""submit"" name=""B1"" value="" 導 入 "" class=""Inputbtn""></p>"
mainbody=mainbody & "<input type=""hidden"" name=""i"" value="&i&"><input type=""hidden"" name=""FilePath"" value="&FilePath&">"
End Function
3、刪除上傳的excel文件
利用FSO組件刪除
call Delete_UpLoadFile(FilePath)
Function Delete_UpLoadFile(FilePath)
Set Fso=server.createobject("scripting.filesystemobject")
If Fso.FileExists(FilePath) then
Fso.DeleteFile FilePath,true
End If
End Function
4、完整的excel導入sql數據庫方法
if request("Action")="upload" then
call UploadFile(fName,FilePath)
call ExcelToSql(fName,FilePath)
call Delete_UpLoadFile(FilePath)
response.redirect "Request.asp"
Response.End
end if
需要注意的幾個地方:
1、請參照example.xls文件的格式進行數據導入,如果excel數據列有增加,可以對程序做適當的修改
2、如果在導入數據時,提示“文件格式有誤”,請檢查ExTName指定的名稱和excel文件里的sheet名稱是否一致;excel文件是否是標準的excel文件格式。
3、確保FilePath在幾個函數之間傳輸時,值不會丟失
4、該程序文件和excel文件在同一目錄下,如果不在同一目錄,在上傳的路徑地方做適當的修改
5、確保everyone對該目錄有讀寫權限,這樣在上傳和刪除文件時不會報錯
6、在導入中文數據時,可能會出現亂碼,中文變問號等情況,可以在讀取數據的頁面頭加入下面的編碼:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
好了,上面就是excel導入sql數據庫方法,對于一些初學者,看到這么多代碼可能就暈,不要怕,找到規律之后,這些代碼也就不難啦,希望大家通過我的介紹能對excel導入sql數據庫有更多的了解。
【編輯推薦】