Link to home
Start Free TrialLog in
Avatar of lghaman123
lghaman123

asked on

Simple Export to Excel with PHP

I wrote the following code to export a multi-dimensional array to rows in an excel spreadsheet for the user to download.  But in the fields where the data is "" or Null, it doesn't tab over to a new row so my columns are getting all jumbled.  Attached is a sample file of what I'm getting.  There must be something small that I am missing but I'm not sure what.  Any help would be appreciated, thanks!
<?php
header("Content-type: application/x-msexcel");
header("Content-Disposition: attachment; filename=report.xls");  
header("Pragma: no-cache");
header("Expires: 0"); 
 
for ($x=1; $x<count($resultArray); $x++) {
	for ($y=0; $y<count($resultArray[$x]); $y++) {
		echo $resultArray[$x][$y]."\t";
	}
	echo "\n";
} 
?>

Open in new window

sample.xls
Avatar of loki23
loki23

try changing:

echo $resultArray[$x][$y]."\t";

to:

echo $resultArray[$x][$y];
echo "\t";

I'm thinking it might have something to do with trying to append the tab to a null value
Avatar of Loganathan Natarajan
You will have to create ROWS + Columns through HTML and export to excel .. just try
logudotcom is right.

excel is pretty thick, unless you give it more to work on as a delimiter. so either use |'s or comma's or do the html method.

but not tab's.

not even swearing at excel works.
ASKER CERTIFIED SOLUTION
Avatar of Loganathan Natarajan
Loganathan Natarajan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lghaman123

ASKER

Thanks guys, I appreciate the help.