Function Close_Conn(SqlConn)
‘關(guān)閉數(shù)據(jù)庫連接并清除數(shù)據(jù)庫連接對象
If IsObject(SqlConn) Then
SqlConn.Close
Set SqlConn = Nothing
End If
End Function
Call Open_Conn("Shat_EDG","","sa","(local)",SqlConn) ‘打開SQL Server數(shù)據(jù)庫連接
Function Get_EMP_CnName(NTACCNT)
‘根據(jù)用戶NT賬號得到用戶中文名
Dim Sql,Rs
Sql = "Select EMP_CNAME From RF_EMPLOYEE Where EMP_NTACCNT=‘"&NTACCNT&"‘"
Set Rs = Server.CreateObject("Adodb.RecordSet")
Rs.Open Sql,SqlConn,1,1
If Rs.Eof Then
Get_EMP_CnName = ""
Else
Get_EMP_CnName = Rs("EMP_CNAME")
End If
Rs.Close
Set Rs = Nothing
End Function %>
<html>
<head>
<title>導(dǎo)入Excel電子表格數(shù)據(jù)到SQL Sever數(shù)據(jù)庫</title>
<body bgcolor="#ACD9AF">
<center><b>導(dǎo)入Excel電子表格數(shù)據(jù)到SQL Sever數(shù)據(jù)庫</b></center>
<FORM METHOD="POST" name="form1">
請選擇數(shù)據(jù)源: <input type="file" name="File" size="40" title="請選擇需要導(dǎo)出的Excel文件"> <input type=submit name=submit value="開始導(dǎo)出">
<br>請選擇目標表:
<SELECT NAME="Table" title="請選擇需要導(dǎo)入數(shù)據(jù)的表">
<option></option>
<% Dim RsSqlDatabaseTable
Set RsSqlDatabaseTable = SqlConn.OpenSchema(20)
Do While Not RsSqlDatabaseTable.Eof %>
<option<%If Trim(Request("Table"))=RsSqlDatabaseTable(2) Then Response.Write " selected"%>><%=RsSqlDatabaseTable(2)%></option>
<% RsSqlDatabaseTable.MoveNext:Loop
Set RsSqlDatabaseTable = Nothing %>
</SELECT>
</FORM>
<font color=blue>導(dǎo)出過程中請不要刷新頁面!</font><br>
<%
If Right(Trim(Request("File")),3) = "xls" Then
Response.Write "<font color=red>您所導(dǎo)出的Excel文件是:"&Trim(Request("File"))&"</font><br>"
End If
‘=====================ASP讀取EXCEL注事項=====================================
‘i)將Excel97或Excel2000生成的XLS文件(book)看成一個數(shù)據(jù)庫,其中的每一個工作表(sheet)看成數(shù)據(jù)庫表
‘ii)ADO假設(shè)Excel中的第一行為字段名.所以你定義的范圍中必須要包括第一行的內(nèi)容
‘iii)Excel中的行標題(即字段名)不能夠包含數(shù)字. Excel的驅(qū)動在遇到這種問題時就會出錯的。例如你的行標題名為“F1”
‘iiii)如果你的Excel電子表格中某一列同時包含了文本和數(shù)字的話,那么Excel的ODBC驅(qū)動將不能夠正常, 處理這一行的數(shù)據(jù)類型,你必須要保證該列的數(shù)據(jù)類型一致
‘E-MAIL:Kaxue@Hotmail.com QQ:484110 HOMEPAGE:www.Flyday.net
‘整理時間:Thursday, May 23, 2002 5:54 PM WIN2000SERVER+IIS5 測式通過
‘============================================================================
If Trim(Request("File")) = "" Then
Response.Write "<font color=red>對不起,請選擇需要導(dǎo)出的Excel文件!</font>"
Response.End
ElseIf Right(Trim(Request("File")),3) <> "xls" Then
Response.Write "<font color=red>對不起,請確定您要導(dǎo)出的是Excel文件!</font>"
Response.End
Else
‘列出所選擇的Excel文件中的所有工作表
Dim ExeclFile,objExcelApp,objExcelBook
ExeclFile = Trim(Request("File"))
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Quit
objExcelApp.DisplayAlerts = False ‘不顯示警告
objExcelApp.Application.Visible = False ‘不顯示界面
objExcelApp.WorkBooks.Open(ExeclFile)
set objExcelBook = objExcelApp.ActiveWorkBook
reDim arrSheets(objExcelBook.Sheets.Count)
For i=1 to objExcelBook.Sheets.Count
arrSheets(i) = objExcelBook.Sheets(i).Name
‘ Response.Write arrSheets(i)
Next
objExcelApp.Quit
Set objExeclApp = Nothing
‘列出所選擇的Excel文件中的所有工作表
End If
Dim Conn,Driver,DBPath,Rs,SqlInsert,SqlDelete
‘建立Connection對象
Set Conn = Server.CreateObject("ADODB.Connection")
Driver = "Driver={Microsoft Excel Driver (*.xls)};"
‘DBPath = "DBQ=" & Server.MapPath( "HRB.xls" )
DBPath = "DBQ=" & Trim(Request("File"))
‘調(diào)用Open 方法打開數(shù)據(jù)庫
Conn.Open Driver & DBPath
‘DSN連接方式
‘Conn.Open "Dsn=test"
‘注意 表名一定要以下邊這種格式 "[表名$]" 書寫
For i = 1 To UBound(arrSheets)‘開始循環(huán)所有工作表
Sql = "SELECT * FROM ["& arrSheets(i) &"$] "
Set Rs = Conn.Execute(Sql)
If Rs.Eof And Rs.Bof Then
Response.write "沒有找到您需要的數(shù)據(jù)!!<br>"
Else
‘ Response.Write "<font color=blue>工作表:</font><font color=green>"&arrSheets(i)&"</font><br>"
Do While Not Rs.Eof
SqlInsert = "Insert Into "&Trim(Request("Table"))&" (EDG_Project_Name,EDG_Project_No,EDG_Project_VM,EDG_Project_VM_CnName,EDG_Project_M,EDG_Project_M_CnName,EDG_Project_Director,EDG_Project_Director_CnName) Values (‘"&Trim(Rs(0))&"‘,‘"&Trim(Rs(1))&"‘,‘"&Trim(Rs(2))&"‘,‘"&Trim(Rs(2))&"("&Get_EMP_CnName(Trim(Rs(2)))&")"&"‘,‘"&Trim(Rs(3))&"‘,‘"&Trim(Rs(3))&"("&Get_EMP_CnName(Trim(Rs(3)))&")"&"‘,‘"&Trim(Rs(4))&"‘,‘"&Trim(Rs(4))&"("&Get_EMP_CnName(Trim(Rs(4)))&")"&"‘)"
‘此處插入的值根據(jù)實際數(shù)據(jù)庫結(jié)構(gòu)進行調(diào)整
SqlConn.Execute(SqlInsert)‘插入Excel表格裏所有東東
Rs.MoveNext
Loop
Response.Write "<font color=red>恭喜,成功導(dǎo)出Excel文件中工作表[</font><font color=blue>"&arrSheets(i)&"</font><font color=red>]的數(shù)據(jù)到SQL Server數(shù)據(jù)表[<font color=blue>"&Trim(Request("Table"))&"</font>]中!^_^</font><br>"
End If
Rs.Close
Set Rs = Nothing
Next‘循環(huán)所有工作表結(jié)束
Call Close_Conn(Conn) ‘關(guān)閉Excel數(shù)據(jù)庫連接
Call Close_Conn(SqlConn) ‘關(guān)閉SQL Server數(shù)據(jù)庫連接 %>
</body>
</html>
聯(lián)系客服