 |
|
ผมขอ สอบถาม เกี่ยวกับการ export excel หน่อยครับ คือผมอยาก เลือก ที่ save ไฟล์ โดย เมื่อเราคลิก ดาวโหลด แล้ว ที่ให้เราเลือก browse folder เพื่อsave file ครับ ได้ว่าเราจะ Save ไฟล์ไว้ที่ไหน โดยที่เราไม่ต้อง ฟิก path ไว้แค่ที่เดียว อยากรู้ว่ามันสามารถทำได้ไหมครับ ขอคำแนะนำด้วยครับ
ต้องการแก้ไขจากโค้ดนี้ครับ phpExportDatabaseToExcel.php
Code (PHP)
<html>
<head>
<title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title>
</head>
<body>
<?
$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 ตรงนี้อยาก เปลี่ยนให้มันเป็น path ตามที่เราต้องการ ไม่อยากให้ฟิกไว้ ที่ไดที่หนึ่ง ครับ
//*** 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.
</body>
</html>
รบกวน พี่ๆ ช่วยแนะนำทีนะครับ
Tag : PHP, Ms SQL Server 2008, Excel (Word.Applcaition)
|
ประวัติการแก้ไข 2012-09-09 19:20:51
|
 |
 |
 |
 |
Date :
2012-09-09 17:26:44 |
By :
arnon011130 |
View :
1074 |
Reply :
2 |
|
 |
 |
 |
 |
|
|
|
 |