tkmanoj Posted June 23, 2008 Share Posted June 23, 2008 I wanted to export a set of data from my database to MS Excel using PHP. I have written a program in PHP, which is working well. But I am having a trouble in the format in which the excel report is being generated. Here is my problem : I have a Employee Table, where the data is as follows: EmpID EmpName Dept 001122 Thomas Marketing 002222 David Production 000345 Joseph Finance But in the excel, I am getting the following data: Emp ID Emp Name Department 1122 Thomas Marketing 2222 David Production 345 Joseph Finance In the database, the EmpID is a Varchar (6), where as in the Excel, it takes as integer. Here is the program I am using: <?php $filename = 'exportdata.xls'; header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=".$filename); header("Pragma: no-cache"); header("Expires: 0"); $out_put = "Emp ID" . "\t" . "Emp Name" . "\t" . "Department" . "\n"; $link = mysql_connect('localhost', 'user1', 'password') or die('Could not connect: ' . mysql_error()); mysql_select_db('test') or die('Could not select database'); $query_emp = 'SELECT * FROM EMPLOYEE'; $result_emp = mysql_query($query_emp) or die('Query failed: ' . mysql_error()); while ($row_emp = mysql_fetch_array($result_emp, MYSQL_ASSOC)) { $EmpID = $row_emp["EmpID"]; $NAME = $row_emp["EMPNAME"]; $DEPT = $row_emp["DEPT"]; $rp_output .= $EmpID . "\t" . $NAME. "\t" . $DEPT . "\n"; } mysql_free_result($result_emp); mysql_close($link); print $out_put; exit; ?> Request your help in fixing this. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/111465-problem-in-data-format-while-export-data-to-excel-using-php/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 23, 2008 Share Posted June 23, 2008 Either use str_pad() or sprintf() to format the $EmpID variable with the leading zeros. Link to comment https://forums.phpfreaks.com/topic/111465-problem-in-data-format-while-export-data-to-excel-using-php/#findComment-572314 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.