自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

excel導(dǎo)入sql數(shù)據(jù)庫(kù)方法

數(shù)據(jù)庫(kù)
excel相信很多人都用過(guò),功能強(qiáng)大,簡(jiǎn)單快捷,sql數(shù)據(jù)庫(kù)在當(dāng)今社會(huì)也是不可忽視的,數(shù)據(jù)庫(kù)帶給社會(huì)的效益還真是無(wú)從說(shuō)起,總之好處多多,那么如何將excel導(dǎo)入sql數(shù)據(jù)庫(kù)?接下來(lái)我就為大家解開(kāi)這個(gè)謎團(tuán)。

導(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"">&nbsp;</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"">&nbsp;總數(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ù)有更多的了解。

【編輯推薦】

  1. 給數(shù)據(jù)減肥 讓MySQL數(shù)據(jù)庫(kù)跑的更快
  2. 安全配置MySQL數(shù)據(jù)庫(kù)有妙招
  3. MySQL數(shù)據(jù)庫(kù)存儲(chǔ)引擎和分支現(xiàn)狀
責(zé)任編輯:迎迎 來(lái)源: 第七大道設(shè)計(jì)網(wǎng)
相關(guān)推薦

2010-10-26 11:04:48

oracle數(shù)據(jù)導(dǎo)入

2010-07-21 14:17:36

SQL Server數(shù)

2009-07-28 11:00:24

Excel導(dǎo)入SQL

2011-07-26 13:05:06

PLSQL DevelopOracle數(shù)據(jù)庫(kù)

2010-11-09 17:19:49

SQL Server導(dǎo)

2011-04-08 09:07:25

Access數(shù)據(jù)庫(kù)Oracle庫(kù)

2010-10-22 11:22:33

SQL Server數(shù)

2010-04-22 11:58:00

Oracle數(shù)據(jù)庫(kù)

2009-03-19 09:44:07

SQL Server數(shù)據(jù)庫(kù)遷移數(shù)據(jù)庫(kù)

2011-08-04 15:55:25

SQL Server數(shù)

2011-03-11 13:26:23

SQL Server數(shù)導(dǎo)入數(shù)據(jù)

2011-08-15 16:58:34

SQL Server遠(yuǎn)程查詢(xún)批量導(dǎo)入數(shù)據(jù)

2022-12-29 08:49:40

SpringBootExcel

2010-09-14 09:53:52

sql server還

2024-04-09 13:20:00

Excel數(shù)據(jù)庫(kù)數(shù)據(jù)

2010-07-21 14:11:36

SQL Server

2010-10-26 15:54:02

連接oracle數(shù)據(jù)庫(kù)

2011-05-13 09:42:21

2010-05-19 15:01:14

MySQL數(shù)據(jù)導(dǎo)入

2009-03-30 10:56:58

SQL Server數(shù)據(jù)庫(kù)死鎖數(shù)據(jù)庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)