ดึงข้อมูลจากดาต้าเบส mysql ลงใน excel ไม่ได้ครับ มัน error
พอดีจะทำการดึงข้อมูลจากดาต้าเบสมาลงใน excel แต่มัน error ประมานว่า path ผิดครับ
นี่ code ครับ
Code (PHP)
<?
$objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
$objDB = mysql_select_db("attendance");
$strSQL = "SELECT * FROM leaves";
$objQuery = mysql_query($strSQL);
if($objQuery)
{
//*** Get Document Path ***//
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$strFileName = "C:/AppServ/www/attendance/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 = "lev_id";
$xlApp->ActiveSheet->Cells(1,2)->Value = "lev_emp_id";
$xlApp->ActiveSheet->Cells(1,3)->Value = "lev_cod_id";
$xlApp->ActiveSheet->Cells(1,4)->Value = "lev_start_date ";
$xlApp->ActiveSheet->Cells(1,5)->Value = "lev_end_date ";
$xlApp->ActiveSheet->Cells(1,6)->Value = "lev_start_time";
//***********//
$intRows = 2;
while($objResult = mysql_fetch_array($objQuery))
{
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["lev_id"];
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["lev_emp_id"];
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["lev_cod_id"];
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["lev_start_date"];
$xlApp->ActiveSheet->Cells($intRows,5)->Value = $objResult["lev_end_date"];
$xlApp->ActiveSheet->Cells($intRows,6)->Value = $objResult["lev_start_time"];
$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.
นี่ส่วน error
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Office Excel<br/><b>Description:</b> The file could not be accessed. Try one of the following: • Make sure the specified folder exists. • Make sure the folder that contains the file is not read-only. • Make sure the file name does not contain any of the following characters: < > ? [ ] : | or * • Make sure the file/path name doesn't contain more than 218 characters.' in C:\AppServ\www\attendance\test.php:39 Stack trace: #0 C:\AppServ\www\attendance\test.php(39): variant->SaveAs('C:\AppServ\www\...') #1 {main} thrown in C:\AppServ\www\attendance\test.php on line 39Tag : PHP, MySQL
ประวัติการแก้ไข 2011-09-26 08:21:52
Date :
2011-09-26 08:20:33
By :
akkachai
View :
1064
Reply :
3
Code นี้ for Office 2003 ครับ
Date :
2011-09-26 08:49:11
By :
webmaster
ผมก็ใช้ office 2003 อ่าครับพี่ ทำไงดี
Date :
2011-09-26 09:07:40
By :
akkchai
งั้นก็ตรวจสอบ Path ให้ดึครับ
Date :
2011-09-26 09:44:55
By :
webmaster
Load balance : Server 00