✔ 最佳答案
首先要寫一個上傳檔案的動作!
可利用AspUpload元件來上傳Excel檔案
<%
Set Upload = Server.CreateObject("Persits.Upload")
Upload.SetMaxSize 5000000, True
Upload.OverwriteFiles = false
On Error Resume Next
UploadFile=Server.MapPath(".") & "\UploadFile\excel\"
Upload.CreateDirectory UploadFile, True
Upload.Save UploadFile
If Err.Number = 8 Then
Response.Write "Your file is too large. Please try again."
Else
If Err <> 0 Then
Response.Write "An error occurred: " & Err.Description
End If
End If
Set File1 = Upload.Files("FILE")
excelPath1="UploadFile/excel/"& FILE1.filename
IF Not File1 Is Nothing Then
Set CN1 = Server.CreateObject("ADODB.Connection")
CN1.Open "DRIVER={MICROSOFT Excel DRIVER (*.xls)};DBQ="&SERVER.MAPPATH(""&excelPath1&"")
SET RS=SERVER.CREATEOBJECT("ADODB.RECORDSET")
SQL="SELECT*From [Sheet1$]"
rs.open SQL,CN1,1,3
%>
通訊錄成功匯入
<% For i = 0 to rs.RecordCount %>
<!-- #INCLUDE file="conn/conn.asp" -->
<%
Set rs1 = Server.CreateObject("ADODB.Recordset")
SQL1="SELECT * FROM addbook where f_name='"&RS.Fields(0).value&"' and types='"&RS.Fields(1).value&"' and phone_id='"&RS.Fields(2).value&"' and userid="&userid&""
RS1.OPEN SQL1,CN,1,3
If rs1.eof Then
rs1.AddNew
RS1("f_name")=RS.Fields(0).value
RS1("types")=RS.Fields(1).value
RS1("phone_id")=RS.Fields(2).value
RS1.UPDATE
Else
RS1("f_name")=RS.Fields(0).value
RS1("types")=RS.Fields(1).value
RS1("phone_id")=RS.Fields(2).value
RS1.UPDATE
End if
t=rs.RecordCount
RS1.CLOSE
CN.close
%>
<% rs.MoveNext %>
<% if rs.eof then exit for
next
%>
<% RS.close %>
<% CN1.close %>
<%
'刪除檔案
Set fs = Server.CreateObject("Scripting.FileSystemObject")
excelfile = Server.MapPath(excelPath1)
fs.DeleteFile excelfile
'Response.Write excelfile
Else
Response.Write "請選擇檔案!"
End If
%>