excel導(dǎo)入sql數(shù)據(jù)庫(kù)方法
導(dǎo)讀:在網(wǎng)上看到很多朋友在咨詢(xún)excel導(dǎo)入sql數(shù)據(jù)庫(kù)方法,那么今天就給大家分享一下實(shí)現(xiàn)的方法。先介紹一個(gè)思路:將excel文件上傳到服務(wù)器,然后預(yù)覽數(shù)據(jù),可以對(duì)數(shù)據(jù)進(jìn)行篩選,點(diǎn)擊“導(dǎo)入”后將選擇的數(shù)據(jù)導(dǎo)入到sql數(shù)據(jù)庫(kù)中,最后把上傳的文件從服務(wù)器上刪除,釋放空間。好了,接下來(lái)我們就按步驟來(lái)講解excel導(dǎo)入sql數(shù)據(jù)庫(kù)方法。
1、excel文件上傳到服務(wù)器
我們采用函數(shù)的實(shí)現(xiàn),代碼如下:
call UploadFile(fName,FilePath)
Function UploadFile(fName,FilePath)
UpLoadFileType="xls|" //設(shè)置允許上傳的文件類(lèi)型
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('文件類(lèi)型錯(cuò)誤');"
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、預(yù)覽數(shù)據(jù),選擇數(shù)據(jù)導(dǎo)入到sql數(shù)據(jù)庫(kù)中
我們采用函數(shù)的實(shí)現(xiàn),代碼如下:
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文件中無(wú)數(shù)據(jù)');"
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"">編號(hào)
</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在處理數(shù)據(jù)類(lèi)型時(shí),前面無(wú)法帶0,補(bǔ)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)
'如果導(dǎo)入的excel數(shù)據(jù)列有變化,請(qǐng)對(duì)程序做適當(dāng)?shù)男薷?/p>
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""> 總數(shù):"&i-1&"</td></tr>"
mainbody=mainbody & "</table><p align=""center""><input type=""submit"" name=""B1"" value="" 導(dǎo) 入 "" 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導(dǎo)入sql數(shù)據(jù)庫(kù)方法
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
需要注意的幾個(gè)地方:
1、請(qǐng)參照example.xls文件的格式進(jìn)行數(shù)據(jù)導(dǎo)入,如果excel數(shù)據(jù)列有增加,可以對(duì)程序做適當(dāng)?shù)男薷?br />
2、如果在導(dǎo)入數(shù)據(jù)時(shí),提示“文件格式有誤”,請(qǐng)檢查ExTName指定的名稱(chēng)和excel文件里的sheet名稱(chēng)是否一致;excel文件是否是標(biāo)準(zhǔn)的excel文件格式。
3、確保FilePath在幾個(gè)函數(shù)之間傳輸時(shí),值不會(huì)丟失
4、該程序文件和excel文件在同一目錄下,如果不在同一目錄,在上傳的路徑地方做適當(dāng)?shù)男薷?br />
5、確保everyone對(duì)該目錄有讀寫(xiě)權(quán)限,這樣在上傳和刪除文件時(shí)不會(huì)報(bào)錯(cuò)
6、在導(dǎo)入中文數(shù)據(jù)時(shí),可能會(huì)出現(xiàn)亂碼,中文變問(wèn)號(hào)等情況,可以在讀取數(shù)據(jù)的頁(yè)面頭加入下面的編碼:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>
好了,上面就是excel導(dǎo)入sql數(shù)據(jù)庫(kù)方法,對(duì)于一些初學(xué)者,看到這么多代碼可能就暈,不要怕,找到規(guī)律之后,這些代碼也就不難啦,希望大家通過(guò)我的介紹能對(duì)excel導(dǎo)入sql數(shù)據(jù)庫(kù)有更多的了解。
【編輯推薦】