日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
excel導入sql數(shù)據(jù)庫方法

導讀:在網上看到很多朋友在咨詢excel導入sql數(shù)據(jù)庫方法,那么今天就給大家分享一下實現(xiàn)的方法。先介紹一個思路:將excel文件上傳到服務器,然后預覽數(shù)據(jù),可以對數(shù)據(jù)進行篩選,點擊“導入”后將選擇的數(shù)據(jù)導入到sql數(shù)據(jù)庫中,最后把上傳的文件從服務器上刪除,釋放空間。好了,接下來我們就按步驟來講解excel導入sql數(shù)據(jù)庫方法。

創(chuàng)新互聯(lián)專注于企業(yè)營銷型網站建設、網站重做改版、八宿網站定制設計、自適應品牌網站建設、H5開發(fā)、商城網站建設、集團公司官網建設、外貿營銷網站建設、高端網站制作、響應式網頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為八宿等各大城市提供網站開發(fā)制作服務。

1、excel文件上傳到服務器

我們采用函數(shù)的實現(xiàn),代碼如下:

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 ""
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 ""
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、預覽數(shù)據(jù),選擇數(shù)據(jù)導入到sql數(shù)據(jù)庫中

我們采用函數(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 ""
Response.end()
End If

if rs_xls.eof then
Set conn_xls = Nothing
set rs_xls=nothing
Delete_UpLoadFile(FilePath)

Response.Write ""
Response.end()
else
i=1
mainbody=mainbody & "

"
mainbody=mainbody & ""
do while not rs_xls.eof

'由于excel在處理數(shù)據(jù)類型時,前面無法帶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數(shù)據(jù)列有變化,請對程序做適當?shù)男薷?/p>

mainbody=mainbody & "

"
mainbody=mainbody & ""
mainbody=mainbody & ""
mainbody=mainbody & ""
mainbody=mainbody & ""
mainbody=mainbody & ""
mainbody=mainbody & ""

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 & "

"
mainbody=mainbody & "
 編號

姓名性別省份
"&payrollcode&""&Rs_xls(1)&""&Rs_xls(2)&""&Rs_xls(3)&"
 總數(shù):"&i-1&"

"
mainbody=mainbody & ""

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數(shù)據(jù)庫方法

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文件的格式進行數(shù)據(jù)導入,如果excel數(shù)據(jù)列有增加,可以對程序做適當?shù)男薷?br /> 2、如果在導入數(shù)據(jù)時,提示“文件格式有誤”,請檢查ExTName指定的名稱和excel文件里的sheet名稱是否一致;excel文件是否是標準的excel文件格式。
3、確保FilePath在幾個函數(shù)之間傳輸時,值不會丟失
4、該程序文件和excel文件在同一目錄下,如果不在同一目錄,在上傳的路徑地方做適當?shù)男薷?br /> 5、確保everyone對該目錄有讀寫權限,這樣在上傳和刪除文件時不會報錯
6、在導入中文數(shù)據(jù)時,可能會出現(xiàn)亂碼,中文變問號等情況,可以在讀取數(shù)據(jù)的頁面頭加入下面的編碼:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%Session.CodePage=936%>

好了,上面就是excel導入sql數(shù)據(jù)庫方法,對于一些初學者,看到這么多代碼可能就暈,不要怕,找到規(guī)律之后,這些代碼也就不難啦,希望大家通過我的介紹能對excel導入sql數(shù)據(jù)庫有更多的了解。


分享題目:excel導入sql數(shù)據(jù)庫方法
文章來源:http://m.5511xx.com/article/cospegj.html