01.
<%
Option
Explicit %>
02.
<html>
03.
<head>
04.
<title>ThaiCreate.Com ASP Excel.Application Tutorial</title>
05.
</head>
06.
<body>
07.
<%
08.
Dim
xlApp,xlBook,xlSheet1,xlSheet2,OpenFile,i
09.
Dim
Conn,strSQL,objExec
10.
OpenFile =
"MyXls/MyCustomer.xls"
11.
12.
13.
Set
xlApp = Server.CreateObject(
"Excel.Application"
)
14.
Set
xlBook = xlApp.Workbooks.Open(Server.MapPath(OpenFile))
15.
Set
xlSheet1 = xlBook.Worksheets(1)
16.
17.
Set
Conn = Server.Createobject(
"ADODB.Connection"
)
18.
Conn.Open
"DRIVER=Microsoft Access Driver (*.mdb);DBQ="
& Server.MapPath(
"MyXls/mydatabase.mdb"
),
""
,
""
19.
20.
For
i = 2
To
5
21.
If
Trim(xlSheet1.Cells.Item(i,1)) <>
""
Then
22.
strSQL =
""
23.
strSQL = strSQL &
"INSERT INTO customer2 "
24.
strSQL = strSQL &
"(CustomerID,Name,Email,CountryCode,Budget,Used) "
25.
strSQL = strSQL &
"VALUES "
26.
strSQL = strSQL &
"('"
&xlSheet1.Cells.Item(i,1)&
"','"
&xlSheet1.Cells.Item(i,2)&
"', '"
&xlSheet1.Cells.Item(i,3)&
"' "
27.
strSQL = strSQL &
",'"
&xlSheet1.Cells.Item(i,4)&
"','"
&xlSheet1.Cells.Item(i,5)&
"', '"
&xlSheet1.Cells.Item(i,6)&
"') "
28.
Set
objExec = Conn.Execute(strSQL)
29.
Set
objExec =
Nothing
30.
End
IF
31.
Next
32.
33.
xlApp.Application.Quit
34.
35.
36.
Conn.Close()
37.
Set
Conn =
Nothing
38.
Set
xlSheet1 =
Nothing
39.
Set
xlBook =
Nothing
40.
Set
xlApp =
Nothing
41.
%>
42.
Data Import/Inserted <a href=
"mydatabase.mdb"
>Click here</a> to Download.
43.
</body>
44.
</html>