|
 |
|
รบกวน สอบถามเรื่อง การ Import Excel แล้วให้แสดงว่า Insert ไปจำนวนกี่เรคคอร์ด ต้องเพิ่มตรงส่วนไหนบ้างครับ |
|
 |
|
|
 |
 |
|
จากโค้ด
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title>
</head>
<body>
<?
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$OpenFile = "MyXls/MyCustomer.xls";
//*** Create Exce.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Open($strPath."/".$OpenFile);
//$xlBook = $xlApp->Workbooks->Open(realpath($OpenFile));
$xlSheet1 = $xlBook->Worksheets(1);
//*** Insert to MSQL Database ***//
$objConnect = mssql_connect("localhost","sa","") or die("Error Connect to Database");
$objDB = mssql_select_db("mydatabase");
for($i=2;$i<=5;$i++){
If(trim($xlSheet1->Cells->Item($i,1)) != "")
{
$strSQL = "";
$strSQL .= "INSERT INTO customer2 ";
$strSQL .= "(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$xlSheet1->Cells->Item($i,1)."','".$xlSheet1->Cells->Item($i,2)."' ";
$strSQL .= ",'".$xlSheet1->Cells->Item($i,3)."','".$xlSheet1->Cells->Item($i,4)."' ";
$strSQL .= ",'".$xlSheet1->Cells->Item($i,5)."','".$xlSheet1->Cells->Item($i,6)."') ";
mssql_query($strSQL);
}
}
//*** Close MSSQL ***//
mssql_close($objConnect);
//*** Close & Quit ***//
$xlApp->Application->Quit();
$xlApp = null;
$xlBook = null;
$xlSheet1 = null;
?>
Data Import/Inserted.
</body>
</html>
ถ้าผมต้องการแสดงว่าได้ Import ไปจำนวนกี่เรคคร์อด จะต้องเพิ่มเติม ตรงส่วนไหนบ้างครับ
ขอบคุณครับ
Tag : PHP, Ms SQL Server 2005

|
|
 |
 |
 |
 |
Date :
2011-03-31 09:35:37 |
By :
paepatter |
View :
877 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Quote:while($xlSheet1->Cells->Item($i,1) != "")
แก้ไขตรงนี้ครับ ถ้าต้องการให้ Import ตามปริมาณจำนวน Data ส่วนจะรู้ว่ากี่ Record ก็เอาค่า $i ไปใช้งานเลยครับ เพราะมัน Loop ตามจำนวนข้อมูลอยู่แล้วครับ
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com Tutorial</title>
</head>
<body>
<?
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
$OpenFile = "MyXls/MyExcelDB.xls";
//*** Create Exce.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Open($strPath."/".$OpenFile);
//$xlBook = $xlApp->Workbooks->Open(realpath($OpenFile));
$xlSheet1 = $xlBook->Worksheets(1);
//*** Insert to MySQL Database ***//
$objConnect = mysql_connect("localhost","root","root") or die(mysql_error());
$objDB = mysql_select_db("mydatabase");
$i=2;
while($xlSheet1->Cells->Item($i,1) != "")
{
$strSQL = "";
$strSQL .= "INSERT INTO customer ";
$strSQL .= "(CustomerID,Name,Email,CountryCode,Budget,Used) ";
$strSQL .= "VALUES ";
$strSQL .= "('".$xlSheet1->Cells->Item($i,1)."','".$xlSheet1->Cells->Item($i,2)."' ";
$strSQL .= ",'".$xlSheet1->Cells->Item($i,3)."','".$xlSheet1->Cells->Item($i,4)."' ";
$strSQL .= ",'".$xlSheet1->Cells->Item($i,5)."','".$xlSheet1->Cells->Item($i,6)."') ";
mysql_query($strSQL);
$i++;
}
//*** Close MySQL ***//
@mysql_close($objConnect);
//*** Close & Quit ***//
$xlApp->Application->Quit();
$xlApp = null;
$xlBook = null;
$xlSheet1 = null;
?>
Data Import/Inserted <?=$i;?> Record(s).
</body>
</html>
Go to : PHP Import Excel to MySQL Database (Excel.Application)
|
 |
 |
 |
 |
Date :
2011-03-31 09:44:05 |
By :
webmaster |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ แต่ผมลงทำดูแล้วเกิด Error อะครับ
Fatal error: Call to a member function Quit() on a non-object in C:\AppServ\www\socal\importexcel\testfile.php on line 50
เป็นที่อะไรหรอครับ
|
 |
 |
 |
 |
Date :
2011-03-31 10:05:42 |
By :
paepatter |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|