下面是代碼部分,但是一直無法測試通過
<%
'On Error Resume Next
'匯入Excel試算表資料到SQL Sever資料庫 By Dicky 2004-12-27 16:41:12 QQ:25941
Function Open_Conn(SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName,SqlConn)
'建立資料庫連接對象並開啟資料庫連接
Dim ConnStr
'SQL Server資料庫連接參數:資料庫名、使用者密碼、使用者名稱、串連名(本地用local,外地用IP)
ConnStr = "Provider=Sqloledb; User ID=" & SqlUsername & "; Password=" & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source=" & SqlLocalName & ";"
Set SqlConn = Server.CreateObject("ADODB.Connection")
SqlConn.Open ConnStr
If Err Then
Err.Clear
Set SqlConn = Nothing
Response.Write "資料連線錯誤!"
Response.End
End If
End Function
Function Close_Conn(SqlConn)
'關閉資料庫連接並清除資料庫連接對象
If IsObject(SqlConn) Then
SqlConn.Close
Set SqlConn = Nothing
End If
End Function
Call Open_Conn("Shat_EDG","","sa","(local)",SqlConn) '開啟SQL Server資料庫連接
Function Get_EMP_CnName(NTACCNT)
'根據使用者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>匯入Excel試算表資料到SQL Sever資料庫</title>
<body bgcolor="#ACD9AF">
<center><b>匯入Excel試算表資料到SQL Sever資料庫</b></center>
<FORM METHOD="POST" name="form1">
請選擇數據源: <input type="file" name="File" size="40" title="請選擇需要導出的Excel檔案"> <input type=submit name=submit value="開始導出">
<br>請選擇目標表:
<SELECT NAME="Table" title="請選擇需要匯入資料的表">
<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>導出過程中請不要重新整理頁面!</font><br>
<%
If Right(Trim(Request("File")),3) = "xls" Then
Response.Write "<font color=red>您所導出的Excel檔案是:"&Trim(Request("File"))&"</font><br>"
End If
'=====================ASP讀取EXCEL注事項=====================================
'i)將Excel97或Excel2000產生的XLS檔案(book)看成一個資料庫,其中的每一個工作表(sheet)看成資料庫表
'ii)ADO假設Excel中的第一行為欄位名.所以你定義的範圍中必須要包括第一行的內容
'iii)Excel中的列名(即欄位名)不能夠包含數字. Excel的驅動在遇到這種問題時就會出錯的。例如你的列名名為“F1”
'iiii)如果你的Excel試算表中某一列同時包含了文本和數位話,那麼Excel的ODBC驅動將不能夠正常, 處理這一行的資料類型,你必須要保證該列的資料類型一致
'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>對不起,請選擇需要導出的Excel檔案!</font>"
Response.End
ElseIf Right(Trim(Request("File")),3) <> "xls" Then
Response.Write "<font color=red>對不起,請確定您要導出的是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"))
'調用Open 方法開啟資料庫
Conn.Open Driver & DBPath
'DSN串連方式
'Conn.Open "Dsn=test"
'注意 表名一定要以下邊這種格式 "[表名$]" 書寫
For i = 1 To UBound(arrSheets)'開始循環所有工作表
Sql = "SELECT * FROM ["& arrSheets(i) &"$] "
Set Rs = Conn.Execute(Sql)
If Rs.Eof And Rs.Bof Then
Response.write "沒有找到您需要的資料!!<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)))&")"&"')"
'此處插入的值根據實際資料庫結構進行調整
SqlConn.Execute(SqlInsert)'插入Excel表格裏所有東東
Rs.MoveNext
Loop
Response.Write "<font color=red>恭喜,成功導出Excel檔案中工作表[</font><font color=blue>"&arrSheets(i)&"</font><font color=red>]的數據到SQL Server數據表[<font color=blue>"&Trim(Request("Table"))&"</font>]中!^_^</font><br>"
End If
Rs.Close
Set Rs = Nothing
Next'循環所有工作表結束
Call Close_Conn(Conn) '關閉Excel資料庫連接
Call Close_Conn(SqlConn) '關閉SQL Server資料庫連接 %>
</body>
</html>
和這個差不多的:
用戶端excel表如何匯入遠程sql server伺服器
<%
'//用戶端上傳Excel,儲存為upload/Excel.xls
.....
'//開啟sql server
....
Conn.Open ...
'//開啟Excel
Set ConnExcel = Server.Createobject("ADODB.Connection")
ExcelStr = "DBQ=" + server.mappath("upload/Excel.xls") + ";DRIVER={Microsoft Excel Driver (*.xls)};"
ConnExcel.Open ExcelStr
Set RSExcel = Server.CreateObject("ADODB.Recordset")
SQLExcel = "Select * From [Sheet1$]" '//你的Sheet1名
RSExcel.Open SQLExcel, ConnExcel, 3, 3
Set RS = Server.CreateObject("ADODB.RecordSet")
SQL = "Select * From [RegUser]"
RS.Open SQL, Conn, 1, 3
'----------------------------
'//添加begin
Do While Not RSExcel.EOF
RS.AddNew
For i=0 To RSExcel.Fields.Count-1
RS(i)=Trim(RSExcel(i))
Next
RS.Update
RSExcel.MoveNext
Loop
'//添加end
'----------------------------
RSExcel.Close
Set RSExcel = Nothing
RS.Close()
Set RS = Nothing
%>