ปัญหาการ PHP Export to Excel แล้า ERROR Uncaught exception 'com_exception
สอบถามพี่ๆ ที่เคยเขียน PHP Export to Excel พอดีผมได้นำโค้ดตัวอย่างเว็บ thaicreate.com ไปลองรันดู แล้วปรากฏว่า Error ดังตัวอย่างนี้ ไม่ทราบว่า ต้องแก้อย่างไรครับ
Code ERROR:
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Office Excel<br/><b>Description:</b> Unable to get the SaveAs property of the Workbook class' in C:\AppServ\www\PHP_Convert_excel\php_excel.php:52 Stack trace: #0 C:\AppServ\www\PHP_Convert_excel\php_excel.php(52): variant->SaveAs('C:\AppServ\www\...') #1 {main} thrown
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer";
$objQuery = mysql_query($strSQL);
if($objQuery)
{
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$strFileName = "MyXls/MyExcel.xls";
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
//*** Create Sheet 1 ***//
$xlBook->Worksheets(1)->Name = "My Customer";
$xlBook->Worksheets(1)->Select;
//*** Header ***//
$xlApp->ActiveSheet->Cells(1,1)->Value = "CustomerID";
$xlApp->ActiveSheet->Cells(1,2)->Value = "Name";
$xlApp->ActiveSheet->Cells(1,3)->Value = "Email";
$xlApp->ActiveSheet->Cells(1,4)->Value = "CountryCode";
$xlApp->ActiveSheet->Cells(1,5)->Value = "Budget";
$xlApp->ActiveSheet->Cells(1,6)->Value = "Used";
//***********//
$intRows = 2;
while($objResult = mysql_fetch_array($objQuery))
{
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["CustomerID"];
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["Name"];
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["Email"];
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["CountryCode"];
$xlApp->ActiveSheet->Cells($intRows,5)->Value = $objResult["Budget"];
$xlApp->ActiveSheet->Cells($intRows,6)->Value = $objResult["Used"];
$intRows++;
}
@unlink($strFileName); //*** Delete old files ***//
$xlBook->SaveAs($strPath."/".$strFileName); //*** Save to Path ***//
//$xlBook->SaveAs(realpath($strFileName)); //*** Save to Path ***//
//*** Close & Quit ***//
$xlApp->Application->Quit();
$xlApp = null;
$xlBook = null;
$xlSheet1 = null;
}
mysql_close($objConnect);
?>
Excel Created <a href="<?=$strFileName?>">Click here</a> to Download.
Tag : PHP, MySQL
Date :
2012-11-26 10:02:16
By :
comtest
View :
1326
Reply :
1
Load balance : Server 03