Export Excel อย่างง่ายกับ PHP

ดูโค้ดก่อนแล้วกัน

<?php
header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="MyXls.xls"');#ชื่อไฟล์
?>

<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">

<HTML>

<HEAD>

<meta http-equiv="Content-type" content="text/html;charset=tis-620" />

</HEAD><BODY>

<TABLE  x:str BORDER="1">

<TR>

<TD><b>AAA</b></TD>

<TD><b>AAA</b></TD>

<TD><b>AAA</b></TD>

</TR>

<TR>

<TD>BBB</TD>

<TD>BBB</TD>

<TD>BBB</TD>

</TR>

<TR>

<TD>001</TD>

<TD>002</TD>

<TD>003</TD>

</TR>

<TR>

<TD>ภาษาไทย</TD>

<TD>ภาษาไทย</TD>

<TD>ภาษาไทย</TD>

</TR>

</TABLE>

</BODY>

</HTML>

จุดหลักๆมีอยู่ 3 จุด
1. แป่ะ Header เพื่อบอกว่าจะเปิดไฟล์ Excel

<?phpheader("Content-Type: application/vnd.ms-excel");header('Content-Disposition: attachment; filename="MyXls.xls"');#ชื่อไฟล์?>

2.ส่วนนี้มีผลกับการแสดง Excel จำเป็นมาก

<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">

3. อันนี้แหล่ะที่จะทำให้ข้อมูลที่นำหน้าด้วยเลขศูนย์แสดงขึ้นมา เพราะกำหนดให้เป็น String

<TABLE  x:str BORDER="1">

ตัวอย่าง คลิกที่นี่
ดาวน์โหลด คลิกที่นี่

อ่านเพิ่มเติมได้ที่นี่ครับ (อธิบายดีจริงๆ) : http://sixhead.com/2008/03/06/easy-export-to-excel/

You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Powered by WordPress | Shop Free T-Mobile Phones Online | Thanks to Verizon Phones without Contract, MMORPG Games and Sell Car