ตัวอย่างการเขียนโคด
โค้ด: เลือกทั้งหมด
<?php
$connection = mysql_connect("localhost", "root", "รหัสผ่าน") or die("เชื่อมต่อฐานข้อมูลไม่ได้") ;
mysql_select_db("ชื่อฐานข้อมูล") or die("ไม่สามารถเลือกฐานข้อมูลได้");
$q = " คำสั่งสำหรับ ดึงข้อมูลจากฐานข้อมูล ";
//echo $action;
$qr = mysql_query($q);
$row_num = mysql_num_rows($qr);
$col_arr = array("username");
$col_num = count($col_arr);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment;filename=data.xls "); //กำหนดชือไฟล์ที่ดาวน์โหลด
?>
<?php echo '<?xml version="1.0" encoding="utf-8"?>'; ?> //กำหนดภาษา
<?php echo'<?mso-application progid="Excel.Sheet"?>'; ?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font x:CharSet="222"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
</Styles>
<Worksheet ss:Name="ข้อมูลจังหวัดในประเทศไทย"> //ชื่อหัวข้อ ไฟล์
<Table ss:ExpandedColumnCount="<?= $col_num ?>" ss:ExpandedRowCount="<?= $row_num + 1 ?>" x:FullColumns="1" x:FullRows="1">
<Row>
//กำหนดชื่อ คอลลัม
<?php foreach ($col_arr as $key => $value) { ?>
<Cell><Data ss:Type="String">ชื่อหัวข้อ</Data></Cell>
<?php } ?>
</Row>
<?php
while ($rs = mysql_fetch_array($qr)) { //ดึงข้อมูลมาแสดง
?>
<Row>
<Cell><Data ss:Type="String">ข้อมูลที่ต้องการแสดง</Data></Cell>
</Row>
<?php } ?>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<Selected/>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>