|
 |
|
ขอถามเรื่อง PHP MySQL to CSV หน่อยครับ ลองทำตามโค้ดที่ให้แล้ว download ไม่ได้ครับ |
|
 |
|
|
 |
 |
|
ลองทำดูแล้วนะครับ แต่ก็ยังไม่ได้ ขึ้นหน้าจอขาวหมดเลย ผมต้องทำตามหัวข้อใน Excel.Application รึปล่าวครับที่ต้องไป config Excel.Application น่ะครับ รบกวนช่วยแนะนำขั้นตอนอย่างละเอียดด้วยครับ ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2012-06-18 16:36:36 |
By :
prapakorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เอา Code ที่คุณเขียนมาดูน่ะครับ 
|
 |
 |
 |
 |
Date :
2012-06-18 17:23:39 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
นี้คือ code ที่พบเอามาจากคุณ mangkunzo ขอออกตัวก่อนว่าผมมือใหม่นะครับ ยังไม่เข้าใจอะไรอีกเยอะ แต่ก็พยายามศึกษาไปเรื่อยๆ ตอนนี้กำลังทำ project ให้สถานประกอบการที่ออกสหกิจอยู่ เหลือแต่ export ข้อมูลออกมา จะเป็น file *.csv หรือ *.xls ก็ได้นะครับ รบกวนช่วยชี้แนะด้วยครับ ขอบคุณตรับ
Code (PHP)
<html>
<head>
<title>test export</title>
</head>
<body bgcolor=#ccffff>
<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("test");
$strSQL = "SELECT * FROM data";
$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 data";
$xlBook->Worksheets(1)->Select;
//*** Header ***//
$xlApp->ActiveSheet->Cells(1,1)->Value = "Date";
$xlApp->ActiveSheet->Cells(1,2)->Value = "Time";
$xlApp->ActiveSheet->Cells(1,3)->Value = "List";
$xlApp->ActiveSheet->Cells(1,4)->Value = "Qty";
$xlApp->ActiveSheet->Cells(1,5)->Value = "PR";
$xlApp->ActiveSheet->Cells(1,6)->Value = "Status";
//***********//
$intRows = 2;
while($objResult = mysql_fetch_array($objQuery))
{
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["Date"];
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["Time"];
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["List"];
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["Qty"];
$xlApp->ActiveSheet->Cells($intRows,5)->Value = $objResult["PR"];
$xlApp->ActiveSheet->Cells($intRows,6)->Value = $objResult["Status"];
$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>
|
 |
 |
 |
 |
Date :
2012-06-18 23:26:18 |
By :
prapakorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
$body="<html xmlns:o='urn:schemas-microsoft-com:office:office'xmlns:x='urn:schemas-microsoft-com:office:excel'xmlns='http://www.w3.org/TR/REC-html40'>";
$body.="ตารางที่ต้องการให้มีใน csv";
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="$file_name"');#ชื่อไฟล์
echo $body;
code ผมมีแค่นี้เองครับมันก็ออกมาเป็นไฟล์ให้ download เรียบร้อย
|
 |
 |
 |
 |
Date :
2012-06-19 17:50:20 |
By :
deer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
export ได้แล้วครับ ขอบคุณทุกท่านที่ช่วยชี้แนะครับ
|
ประวัติการแก้ไข 2012-06-21 15:28:10 2012-06-21 15:29:13 2012-06-21 15:29:51
 |
 |
 |
 |
Date :
2012-06-21 11:25:37 |
By :
prapakorn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|