This is how we can save our php work to microsoft excel file .xls.
This is the code :
<?
// Connect database.
mysql_connect("localhost","user_db","db_password");
mysql_select_db("db_name");
// Get data records from table.
$result=mysql_query("select * from reseller ");
// Functions for export to excel.
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: filename=mycustomer.xls ");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at '0'
*/
xlsWriteLabel(0,0,"List of My customer");
// Make column labels. (at line 3)
xlsWriteLabel(2,0,"No.");
xlsWriteLabel(2,1,"First Name");
xlsWriteLabel(2,2,"Last Name");
xlsWriteLabel(2,3,"Address");
xlsWriteLabel(2,4,"Email");
$xlsRow = 3;
// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result))
{
xlsWriteNumber($xlsRow,0,$row['user_id']); // create number
xlsWriteLabel($xlsRow,1,$row['first_name']);
xlsWriteLabel($xlsRow,2,$row['last_name']);
xlsWriteLabel($xlsRow,3,$row['address']);
xlsWriteLabel($xlsRow,4,$row['email']);
$xlsRow++;
}
xlsEOF();
exit();
?>
With this code :
you will save new xls file named : mycustomer.xls
You can use and modify it for your own purpose.
Tuppy free photo sharing site
Incoming search terms for the article:
save xlswritelabel,
xlsWriteLabel how to save