Page 1 of 1
export table to excel in php
Posted: Thu Oct 30, 2008 11:17 am
by kabucek
Hi @LL
I want export table in php to excell file.
Here is for example link:
Code: Select all
https://mydomain.local/something.php?op ... le1.field3
and this display 5 fields with maximum of 10 rows.
Is there a way to export to excel file using above link?
Our framework use sql & php.
It would be great if this could be a hyperlink with text: "click to save"
or a button?
if need more info let me know.
Thanks
Re: export table to excel in php
Posted: Thu Oct 30, 2008 1:17 pm
by aceconcepts
Below is a procedure i use when i download data from MySQL DB using PHP
Code: Select all
// 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: attachment;filename=your_file_name.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 registrations.");
// Make column labels. (at line 3)
xlsWriteLabel(0,0,"First Name");
xlsWriteLabel(0,1,"Last Name");
$xlsRow = 1;
//get table data
$sql=mysql_query("SELECT fields FROM your_table ORDER BY whatever");
// Put data records from mysql by while loop.
while($row=mysql_fetch_array($sql))
{
xlsWriteLabel($xlsRow,0,$row['strFirstName']);
xlsWriteLabel($xlsRow,1,$row['strLastName']);
$xlsRow++;
}
xlsEOF();
exit();
Re: export table to excel in php
Posted: Thu Oct 30, 2008 1:22 pm
by kabucek
thanks
this is nice,
but is there a way that I can just simply use my link?