Jump to content

Problem in Data format while Export Data to Excel using PHP


tkmanoj

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.