01.
<html>
02.
<head>
03.
<title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title>
04.
</head>
05.
<body>
06.
<?
07.
08.
$strPath
=
realpath
(
basename
(
getenv
(
$_SERVER
[
"SCRIPT_NAME"
])));
09.
$OpenFile
=
"MyXls/MyCustomer.xls"
;
10.
11.
$xlApp
=
new
COM(
"Excel.Application"
);
12.
$xlBook
=
$xlApp
->Workbooks->Open(
$strPath
.
"/"
.
$OpenFile
);
13.
14.
15.
$xlSheet1
=
$xlBook
->Worksheets(1);
16.
17.
18.
$objConnect
= mysql_connect(
"localhost"
,
"root"
,
"root"
)
or
die
(
"Error Connect to Database"
);
19.
$objDB
= mysql_select_db(
"mydatabase"
);
20.
21.
for
(
$i
=2;
$i
<=5;
$i
++){
22.
If(trim(
$xlSheet1
->Cells->Item(
$i
,1)) !=
""
)
23.
{
24.
$strSQL
=
""
;
25.
$strSQL
.=
"INSERT INTO customer2 "
;
26.
$strSQL
.=
"(CustomerID,Name,Email,CountryCode,Budget,Used) "
;
27.
$strSQL
.=
"VALUES "
;
28.
$strSQL
.=
"('"
.
$xlSheet1
->Cells->Item(
$i
,1).
"','"
.
$xlSheet1
->Cells->Item(
$i
,2).
"' "
;
29.
$strSQL
.=
",'"
.
$xlSheet1
->Cells->Item(
$i
,3).
"','"
.
$xlSheet1
->Cells->Item(
$i
,4).
"' "
;
30.
$strSQL
.=
",'"
.
$xlSheet1
->Cells->Item(
$i
,5).
"','"
.
$xlSheet1
->Cells->Item(
$i
,6).
"') "
;
31.
mysql_query(
$strSQL
);
32.
}
33.
}
34.
35.
36.
@mysql_close(
$objConnect
);
37.
38.
39.
$xlApp
->Application->Quit();
40.
$xlApp
= null;
41.
$xlBook
= null;
42.
$xlSheet1
= null;
43.
?>
44.
Data Import/Inserted.
45.
</body>
46.
</html>