Jump to content

Export to excel issue!


iPixel

Recommended Posts

So i have this script, it pull data via mysql and inserts it into an excel file.

However it does not properly tab each field as it should nor go to a new lines as it should instead it stick everything into 1 whole row.

 

Ok but here's the kicker... this same identical script runs 100% fine on another website. The only difference is the query string. There it works fine on this new site it bugs out.

 

any idea where the bug is?

 

<?php
include('dbcon.php');

$tablename = $_POST['tablename'];

$full_query = "SELECT * FROM $tablename";   

$header = '';
$data = '';
$export = mysql_query($full_query);
$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/octet-stream"); 
header("Content-Disposition: attachment; filename=monet_search_results.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
print "$header\n$data"; 

?>

 

PS: This is a linux box if that makes a difference.

 

THANKS!

Link to comment
https://forums.phpfreaks.com/topic/228009-export-to-excel-issue/
Share on other sites

i suspect it's the line-endings. you can try \n and/or \r\n instead. but i really suggest that you try phpexcel. it is a very easy-to-use class that makes real Excel files (not just tab-delimited as in your code). you can add worksheets, cell styles, all kinds of stuff if you need to.

 

http://phpexcel.codeplex.com/

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.