PDA

View Full Version : export in excel from php/mysql



websoftexpert
09-29-2008, 01:15 PM
Hi

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 http://64.131.70.95/~srijan/orangehrm/templates/time/excelTest.php. The source of file is below.

=============================================

<?php
ini_set("session.gc_maxlifetime",3600);
ini_set("session.bug_compat_warn",0);
ini_set("error_reporting",'E_ALL & ~(E_WARNING | E_NOTICE | E_USER_NOTICE)');

ob_start();

session_start();




$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>";
//exit;
//$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>";
//exit;
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";
}else{
# 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.

thanks

A Sinha

techietim
09-29-2008, 07:07 PM
http://sourceforge.net/projects/php-doc-xls-gen/