Re: export data to a ms excel file using php

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Dec 18, 2007, at 4:05 AM, abderrazzak nejeoui wrote:

can you help me to export data to a ms excel file using php. i tried to export them to an html format and change the extension to .xls that's work
but i have lost the formatting

excel and the navigator doesn't interpret my code in the same why (celles
are not in the same size)


Hi Abderrazzak,

This is a script that I use to export to excel, and it works quite well after pulling the info from a Database.

If you find any issues with it, let me know so I can try and fix it! But I haven't had any complaints about it yet.

<?PHP

$sortOrder = $_SESSION['order'];
$search = $_SESSION['search'];
$select = "SELECT * FROM ".$table." WHERE FName like '%".$search."%' or LName like '%".$search."%' or Add1 like '%".$search."%' or Add2 like '%".$search."%' or City like '%".$search."%' or State like '%". $search."%' or Zip like '%".$search."%' or XCode like '%".$search."%' order by ".$sortOrder."";

$export = mysql_query($select);
$fields = mysql_num_fields($export);

for ($i = 0; $i < $fields; $i++) {
	$header .= mysql_field_name($export, $i) . "\t";
}

while($row = mysql_fetch_row($export)) {
	$line = '';
	foreach($row as $value) {
		if ((!isset($value)) or ($value == "")) {
			$value = "\t";
		}
		else
		{
			$value = str_replace('"', '""', $value);
			$value = '"' . $value . '"' . "\t";
		}	
		$line .= $value;
	}
	$data .= trim($line). "\n";
}
$data = str_replace("\r", "", $data);

if ($data =="") {
	$data ="\n(0) Records Found!\n";
}
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=Export.xls");
header("Pragma: no-cache");
header("Expires: 0");


print "$header\n$data";


?>

--

Jason Pruim
Raoset Inc.
Technology Manager
MQC Specialist
3251 132nd ave
Holland, MI, 49424
www.raoset.com
japruim@xxxxxxxxxx

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux