View Full Version : export in excel from php/mysql

09-29-2008, 01:15 PM

I am using a script for exporting in excel from php/mysql. It is working fine on my local window server by on my linux server it is not generation excel file for download. Here is url to check The source of file is below.


ini_set("error_reporting",'E_ALL & ~(E_WARNING | E_NOTICE | E_USER_NOTICE)');



$query="Select B.employee_id, A.emp_firstname, A.emp_middle_name, A.emp_lastname, B.start_date, B.end_date from hs_hr_employee as A, hs_hr_timesheet as B where B.employee_id=A.emp_number and B.status = 10 and B.start_date>='2008-01-01' and ( B.employee_id= 9 or B.employee_id= 31 or B.employee_id= 66 ) order by A.emp_firstname ASC,B.start_date desc,B.timesheet_id desc";
require_once '/connection.php';

//print $query."<P>";
//$result = mysql_query('select * from hs_hr_users');
$result = mysql_query(str_replace('\\', '', $query)) or die(mysql_error());
$count = mysql_num_fields($result);
//print $count."<P>";
for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";

while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
$line .= $value;
$data .= trim($line)."\n";
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);

# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";

# This line will stream the file to the user rather than spray it across the screen
//header("Content-type: application/octet-stream");

header ("Content-type: application/x-msexcel");
# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=unapprovedTimesheet" . gmdate("D d M YH:i:s") . ".xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;

require_once '../../footer.php';



Can u help me to find out bug and solve it.


A Sinha

09-29-2008, 07:07 PM