ASP網頁EXCEL檔上傳匯入access資料庫

2010-05-06 11:44 pm
我想把excel檔,在ASP網頁中,點選上傳後,匯入資料庫,請問「EXCEL上傳,匯入資料庫」的動作,要怎麼寫呢?

回答 (2)

2010-05-07 7:28 pm
✔ 最佳答案
首先要寫一個上傳檔案的動作!
可利用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
%>
參考: Me
2015-05-07 2:37 am
網上看了這家,因為我公司的硬碟故障,微軟的張經理都找過他們,把他們送去內湖救不出來的,都給救出來
http://www.datamaster.com.tw/
http://millerforyew1.pixnet.net/blog/


收錄日期: 2021-04-30 22:12:57
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100506000016KK03908

檢視 Wayback Machine 備份