 |
เครียดมาก ช่วยดูการสร้างไฟล์ excel จากฐานข้อมูลให้หน่อยค่ะ ทำเท่าไหร่ก็ไม่ได้ |
|
 |
|
|
 |
 |
|
Code (PHP)
<?
include("../../utility.php");
connect_db();
$month = $_REQUEST["select_month"];
$year = $_REQUEST["select_year"];
?>
<html>
<head>
<title>Print Report</title>
</head>
<body>
<?
$sql = "SELECT datatable.data_id, datatable.hnNum, doctable.doc_name, proceduretable.proc_name, specialty_table.specialty_name,datatable.room_id, datatable.timeStartReserve, datatable.timeUseReserve, datatable.dateReserve, datatable.status_id, datatable.room_actual, datatable.timeUseAc, datatable.timeStartAc FROM datatable, proceduretable, specialty_table, doctable WHERE MONTH(dateReserve) = $month AND YEAR(dateReserve) = $year AND datatable.doc_id = doctable.doc_name AND datatable.proc_id = proceduretable.proc_id AND datatable.specialty_id = specialty_table.specialty_id ORDER BY datatable.dateReserve";
$result = mysql_query($sql) or die(mysql_error());
if($result)
{
//*** Get Document Path ***/
$strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp
//*** Excel Document Root ***//
$strFileName = "report.xls";
//*** Connect to Excel.Application ***//
$xlApp = new COM("Excel.Application");
$xlBook = $xlApp->Workbooks->Add();
//*** Create Sheet 1 ***//
$xlBook->Worksheets(1)->Name = "report";
$xlBook->Worksheets(1)->Select;
//*** Header ***//
$xlApp->ActiveSheet->Cells(1,1)->Value = "Case_ID";
$xlApp->ActiveSheet->Cells(1,2)->Value = "HN Number";
$xlApp->ActiveSheet->Cells(1,3)->Value = "Doctor_name";
$xlApp->ActiveSheet->Cells(1,4)->Value = "Procedure";
$xlApp->ActiveSheet->Cells(1,5)->Value = "Specialty";
$xlApp->ActiveSheet->Cells(1,6)->Value = "Room_Plan";
$xlApp->ActiveSheet->Cells(1,7)->Value = "Date";
$xlApp->ActiveSheet->Cells(1,8)->Value = "Time Start Planning";
$xlApp->ActiveSheet->Cells(1,9)->Value = "Time Used Planning";
$xlApp->ActiveSheet->Cells(1,10)->Value = "Room_Actual";
$xlApp->ActiveSheet->Cells(1,11)->Value = "Time Start Actual";
$xlApp->ActiveSheet->Cells(1,12)->Value = "Time Used Actual";
$xlApp->ActiveSheet->Cells(1,13)->Value = "Cancel";
//***********//
$intRows = 2;
while($objResult = mysql_fetch_array($result))
{
//*** Detail ***//
$xlApp->ActiveSheet->Cells($intRows,1)->Value = $objResult["data_id"];
$xlApp->ActiveSheet->Cells($intRows,2)->Value = $objResult["hnNum"];
$xlApp->ActiveSheet->Cells($intRows,3)->Value = $objResult["doc_name"];
$xlApp->ActiveSheet->Cells($intRows,4)->Value = $objResult["proc_name"];
$xlApp->ActiveSheet->Cells($intRows,5)->Value = $objResult["specialty_name"];
$xlApp->ActiveSheet->Cells($intRows,6)->Value = $objResult["room_id"];
$xlApp->ActiveSheet->Cells($intRows,7)->Value = $objResult["dateReserve"];
$xlApp->ActiveSheet->Cells($intRows,8)->Value = $objResult["timeStartReserve"];
$xlApp->ActiveSheet->Cells($intRows,9)->Value = $objResult["timeUseReserve"];
$xlApp->ActiveSheet->Cells($intRows,10)->Value = $objResult["room_actual"];
$xlApp->ActiveSheet->Cells($intRows,11)->Value = $objResult["timeStartAc"];
$xlApp->ActiveSheet->Cells($intRows,12)->Value = $objResult["timeUseAc"];
$xlApp->ActiveSheet->Cells($intRows,13)->Value = ($objResult["status_id"] == 7 ? "Yes":"No");
$intRows++;
}
@unlink($strFileName); //*** Delete old files ***//
echo "C:/Users/Nantaya/Desktop/".$strFileName;
$xlBook->SaveAs("C:/Users/Nantaya/Desktop/".$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, MySQL
|
|
 |
 |
 |
 |
Date :
2012-04-13 20:49:00 |
By :
yam19 |
View :
1354 |
Reply :
13 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่บรรทัดนี้ => $xlApp = new COM("Excel.Application"); ไม่น่าจำทำงานได้นะครับ
เพราะบน Server ไม่น่าจะรู้จัก COM object และถ้าไม่ได้ลงไฟล์ Excel ด้วยแล้วก็ไม่มีทางเป็นไปได้เลยครับ
แต่ถ้าต้องการสร้างเป็น Excel จริงๆ ผมแนะนำให้ใช้ ลองใช้ XML for Excel 2003 ดูนะครับ วิธีการก็คือ
สร้าง Excel ตาม Pattern ที่คุณต้องการก่อน จากนั้นก็ลอง Save as เป็น XML Spreadsheet 2003 จากนั้น
ก็ลอง Edit ด้วย Notepad หรือ Text Editor อื่นๆ ดู แล้วก็ลองศึกษาดูครับมันจะเป็น Pattern เลยลองดูครับ
|
 |
 |
 |
 |
Date :
2012-04-14 00:21:46 |
By :
smeproject |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากค่ะ แต่ทำไม่เป็นอ่ะค่ะ
|
 |
 |
 |
 |
Date :
2012-04-15 00:28:04 |
By :
yam19 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ลงโปรแกรม microsoft office ที่ server หรือยังครับ
|
 |
 |
 |
 |
Date :
2012-04-18 15:16:20 |
By :
คนผ่านมา |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เปลี่ยนมาเขียนลง csv ง่ายกว่าเยอะ
|
 |
 |
 |
 |
Date :
2012-04-18 16:14:39 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
อย่าลืม server ต้องเปิด zip, แล้วก็อะไรอีกก็ไม่รู้ แต่ผมก็ใช้ตัวนี้เหมือนกัน
|
 |
 |
 |
 |
Date :
2012-04-18 18:26:33 |
By :
ikikkok |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Software requirements
The following software is required to develop using PHPExcel:
» PHP version 5.2.0 or newer
» PHP extension php_zip enabled *)
» PHP extension php_xml enabled
» PHP extension php_gd2 enabled (if not compiled in)
แต่ว่าผมใช้บน PHP 5.1 ก็ใช้ได้ครับ แต่ว่าใช้ได้แค่ Excel 2003, html (PDF ไม่แน่ใจว่าได้ป่าว)
และก็มันจะทำให้ไม่สามารถกำหนด format cell เป้นพวก datetime ของexcel ได้
เพราะว่ามันต้องใช้ฟังก์ชันที่มีใน 5.2 ครับ
ประมาณนี้แหละ
|
 |
 |
 |
 |
Date :
2012-04-19 17:23:53 |
By :
naskw |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใครพอจะอนุเคราะห์ตัวอย่างดึง mysql แล้ว export เป็น XLS โดยใช้คลาสตัวนี้บ้างครับ http://phpexcel.codeplex.com/
|
 |
 |
 |
 |
Date :
2012-04-25 22:21:13 |
By :
วงศ์กร |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณ พี่ๆ นะคะ codeนี้ http://phpexcel.codeplex.com/ เจ๋งมากจริง ตอบโจทย์   
|
 |
 |
 |
 |
Date :
2012-05-04 11:31:43 |
By :
4i4 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|