การเขียน php ออก report หรือ export เป็น excel รูปแบบไฟล์ 2 แบบ คือ .xls กับ .csv
ก่อนจะเริ่มดูโค้ด php มาทำความเข้าใจกันก่อนนะครับ
ในการเปิดหน้าเว็บจาก Browser ไม่ว่าจะเป็น IE,Firefox หรือ Browser ตัวอื่นๆ
ข้อมูลที่ เจ้า Browser มันมองจะมีสองส่วนคือ ส่วนของ Header กับ Data
1. Header คือ ส่วนที่ Browser จะดูว่าออกสาร หรือหน้าเว็บที่มันกำลังเปิดนั้นเป็นอะไร (โดยปกติจะเป็น text/html)
2. Data คือส่วนที่เป็นข้อมูลที่เราเห็น เช่นหน้าเว็บที่เราเห็น ถ้าเป็นเว็บเพจก็ จะเป็น tag html ซึ่ง browser จะแปลงเป็นรูปแบบที่เราเห็นๆ กันอยู่ดังตัวอย่าง
ของเว็บนี้ mindphp.com
ต่อไป มาดูว่าแล้วเราจะทำไงให้ php ของเราส่ง Header ให้ Browser เข้าใจว่าเป็น xls ไฟล์
เราต้องใช้ function header() ของ php เข้ามาช่วย
ตัวอย่าง
header("Content-Type: application/x-msexcel);
ต่อไปก็จะเป็น ส่วนของ Data เมื่อเรากำหนด ส่วนของ header ให้ Browser รู้แล้วว่าเราจะส่งข้อมูลที่ ต้องเปิดกับ โปรแกรม MS excel แล้วเราก็สามารถ
สร้าง ข้อมูล ด้วย php โดยอาจจะดึงข้อมูลจาก ฐานข้อมูล หรือ จากการคำนวนใน php ส่งออกไป
ข้อมูลที่จะส่งออกไป ผมขอแยกเป็น 2 แบบ คือ
1. ข้อมูลที่ เป็น text ธรรมดา
2. ข้อมูลที่เป็น รูปแบบ เฉพาะ ของ MS excel เช่น ใน celll มีสูตร หรือ กำนหนดให้แสดงเป็นค่า เงิน หรือ กำหนด แสดงเป็นวันเดือนปี หรือ ฯลฯ ตามความสามารถของ MS excel ที่มันทำได้ (แต่จะไม่รวม มาโครนะครับ)
แบบแรก
1.ทำข้อมูลเป็น text? โดยมี comma (,) ขั้น เรียกว่า csv ไฟล์
2. เป็นรูปแบบ tag html
3. ทำเป็น XML
อาจจะมีรูปแบบอื่นอีก? โดยข้อมูลพวกนี้ อย่างที่เรารู้กันอยู่แล้วคือ สามารถ echo หรือ print ค่าส่งให้ browser ได้เลย
แบบที่สอง
ข้อมูลรูปแบบเฉพาะของ MS excel ซึ่งต้องอาศัย lib เฉพาะ ที่จะแนะนำ มีอยู่ สองตัวคือ
1. สร้าง Object จาก new COM("Excel.application");?? ใช้ได้เฉพาะ Server ที่เป็น windows ซึ่งใน Object จะมี methode ให้ใช้งาน
ตัวอย่าง โค้ด
=====================================
// Starting Excel
header('Content-Type: application/x-msexcel');
header('Content-Disposition: inline; filename="mindphp_com.xls"');??? // ตั้งชื่อไฟล์
header("Pragma: no-cache");??? ??? ??? // บอก Browser ว่าไม่ต้อง เก็บ cache
$xlsApp = new COM("Excel.Application") or die("Unable to instanciate excel");
$xlsApp->Visible = 0;
$xlsApp->DisplayAlerts = 0;
// ข้อมูลที่จะเขียน ลง xls ไฟล์
echo 'Mindphp.com';
$xlsApp->Quit();
unset($xlsApp);
?>
===================================
2. Class write excel ซึ่งก็มีหลายตัว
เช่น
php_writeexcel Class
ตัวอย่าง โค้ด การ Export เป็น Excel ด้วย php_writeexcel Class
# Demonstrates Spreadsheet::WriteExcel's named colors and the Excel
# color palette.
#
# reverse('ฉ'), March 2002, John McNamara, [email protected]
# PHP port by Johann Hanne, 2005-11-01
set_time_limit(10);
require_once "class.writeexcel_workbook.inc.php";
require_once "class.writeexcel_worksheet.inc.php";
$fname = tempnam("/tmp", "colors.xls");
$workbook = &new writeexcel_workbook($fname);
# Some common formats
$center? =& $workbook->addformat(array('align' => 'center'));
$heading =& $workbook->addformat(array('align' => 'center', 'bold' => 1));
# Try this to see the default Excel 5 palette
# $workbook->set_palette_xl5();
######################################################################
#
# Demonstrate the named colors.
#
$colors = array(
'black'=>0x08,
'blue'=>0x0C,
'brown'=>0x10,
'cyan'=>0x0F,
'gray'=>0x17,
'green'=>0x11,
'lime'=>0x0B,
'magenta'=>0x0E,
'navy'=>0x12,
'orange'=>0x35,
'purple'=>0x14,
'red'=>0x0A,
'silver'=>0x16,
'white'=>0x09,
'yellow'=>0x0D
);
$worksheet1 =& $workbook->addworksheet('Named colors');
$worksheet1->set_column(0, 3, 15);
$worksheet1->write(0, 0, "Index", $heading);
$worksheet1->write(0, 1, "Index", $heading);
$worksheet1->write(0, 2, "Name",? $heading);
$worksheet1->write(0, 3, "Color", $heading);
$i = 1;
foreach ($colors as $color=>$index) {
$format =& $workbook->addformat(array(
'fg_color' => $color,
'pattern'? => 1,
'border'?? => 1
));
$worksheet1->write($i+1, 0, $index,??????????????????? $center);
$worksheet1->write($i+1, 1, sprintf("0x%02X", $index), $center);
$worksheet1->write($i+1, 2, $color,??????????????????? $center);
$worksheet1->write($i+1, 3, '',??????????????????????? $format);
$i++;
}
######################################################################
#
# Demonstrate the standard Excel colors in the range 8..63.
#
$worksheet2 =& $workbook->addworksheet('Standard colors');
$worksheet2->set_column(0, 3, 15);
$worksheet2->write(0, 0, "Index", $heading);
$worksheet2->write(0, 1, "Index", $heading);
$worksheet2->write(0, 2, "Color", $heading);
$worksheet2->write(0, 3, "Name",? $heading);
for ($i=8;$i<=63;$i++) {
$format =& $workbook->addformat(array(
'fg_color' => $i,
'pattern'? => 1,
'border'?? => 1
));
$worksheet2->write(($i -7), 0, $i,??????????????????? $center);
$worksheet2->write(($i -7), 1, sprintf("0x%02X", $i), $center);
$worksheet2->write(($i -7), 2, '',??????????????????? $format);
# Add the? color names
foreach ($colors as $color=>$index) {
if ($i==$index) {
$worksheet2->write(($i -7), 3, $color, $center);
}
}
}
$workbook->close();
header("Content-Type: application/x-msexcel; name="mindphp-colors.xls"");
header("Content-Disposition: inline; filename="mindphp-colors.xls"");
$fh=fopen($fname, "rb");
fpassthru($fh);
unlink($fname);
?>
ตัวอย่าง จาก bettina-attack.de
example-bigfile.php (source code)
example-colors.php (source code)
example-demo.php (source code)
example-merge2.php (source code)
example-panes.php (source code)
example-repeat.php (source code)
example-simple.php (source code)
example-stocks.php (source code)
example-textwrap.php (source code)