01.
<html>
02.
<head>
03.
<title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title>
04.
<meta http-equiv=
"Content-Type"
content=
"text/html; charset=utf-8"
></head>
05.
<body>
06.
<?php
07.
set_time_limit(0);
08.
09.
$strPath
=
realpath
(
basename
(
getenv
(
$_SERVER
[
"SCRIPT_NAME"
])));
10.
$OpenFile
=
"MyXls/18-05-2015.xlsx"
;
11.
12.
$xlApp
=
new
COM(
"Excel.Application"
, NULL, CP_UTF8)
or
Die (
"Did not instantiate Excel"
);
13.
$xlBook
=
$xlApp
->Workbooks->Open(
$strPath
.
"/"
.
$OpenFile
);
14.
15.
16.
$xlSheet1
=
$xlBook
->Worksheets(1);
17.
$count
=
$xlSheet1
->UsedRange->Rows->
Count
();
18.
19.
20.
include
"conex.php"
;
21.
$PlanDate
=
$xlSheet1
->Cells->Item(1,1);
22.
$lp
=
strlen
(
$PlanDate
);
23.
$planD
=
substr
(
$PlanDate
, 62,
$lp
);
24.
$newplanD
=
ereg_replace
(
'[[:space:]]+'
,
' '
, trim(
$planD
));
25.
for
(
$i
=5;
$i
<=150;
$i
++){
26.
if
(trim(
$xlSheet1
->Cells->Item(
$i
,2)) !=
""
)
27.
{
28.
$weight
= number_format(
$xlSheet1
->Cells->Item(
$i
,13) , 2 );
29.
30.
31.
$strSQL
=
""
;
32.
$strSQL
.=
"INSERT INTO plantable "
;
33.
$strSQL
.=
"(NO,PRD,SO,CusName,ProName,Weight,ShipDate,OrderNum,ShipNum,CarReg,PlanDate,M) "
;
34.
$strSQL
.=
"VALUES "
;
35.
$strSQL
.=
"('"
.
$xlSheet1
->Cells->Item(
$i
,2).
"','"
.
$xlSheet1
->Cells->Item(
$i
,4).
"','"
.
$xlSheet1
->Cells->Item(
$i
,5).
"' "
;
36.
$strSQL
.=
",'"
.
$xlSheet1
->Cells->Item(
$i
,6).
"','"
.
$xlSheet1
->Cells->Item(
$i
,10).
"' "
;
37.
$strSQL
.=
",'$weight','"
.
$xlSheet1
->Cells->Item(
$i
,16).
"' "
;
38.
$strSQL
.=
",'"
.
$xlSheet1
->Cells->Item(
$i
,17).
"','"
.
$xlSheet1
->Cells->Item(
$i
,18).
"','"
.
$xlSheet1
->Cells->Item(
$i
,26).
"','$newplanD','"
.
$xlSheet1
->Cells->Item(
$i
,12).
"') "
;
39.
mysql_query(
$strSQL
);
40.
}
41.
42.
}
43.
44.
45.
46.
47.
@mysql_close(
$objConnect
);
48.
49.
50.
$xlApp
->Application->Quit();
51.
$xlApp
= null;
52.
$xlBook
= null;
53.
$xlSheet1
= null;
54.
?>
55.
Data Import/Inserted.
56.
</body>
57.
</html>