日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

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

1、excel文件上傳到服務(wù)器

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

call UploadFile(fName,FilePath)

Function UploadFile(fName,FilePath)

UpLoadFileType="xls|" //設(shè)置允許上傳的文件類型
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、預(yù)覽數(shù)據(jù),選擇數(shù)據(jù)導(dǎo)入到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)

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

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

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


網(wǎng)頁名稱:excel導(dǎo)入sql數(shù)據(jù)庫方法
URL鏈接:http://www.dlmjj.cn/article/cospegj.html