Export MySQL data records to an Excel file in one PHP file, simple
and easily!
This tutorial require 1 PHP file and 1 table of mySQL database.
- export_excel.php
- Database "tutorial" and table "name_list" with 2 fields:
id(auto_increment), name(varchar, 50) and put some records about 20 -
30 records into this table. (directly by phpMyAdmin)
<?
mysql_connect("localhost","","");
mysql_select_db("tutorial");
$result=mysql_query("select * from name_list order by id asc");
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
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=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
xlsWriteLabel(0,0,"List of car company.");
xlsWriteLabel(2,0,"No.");
xlsWriteLabel(2,1,"Company");
$xlsRow = 3;
while($row=mysql_fetch_array($result)){
xlsWriteNumber($xlsRow,0,$row['id']);
xlsWriteLabel($xlsRow,1,$row['name']);
$xlsRow++;
}
xlsEOF();
exit();
?>
|
|