Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple Export to Excel with PHP

Posted on 2008-06-18
5
Medium Priority
?
643 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:lghaman123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Expert Comment

by:loki23
ID: 21817867
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
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 21819103
You will have to create ROWS + Columns through HTML and export to excel .. just try
0
 

Expert Comment

by:cfpdev
ID: 21820931
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.
0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 2000 total points
ID: 21820978
Ok try something like
<?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++) {
       echo "<tr>";
	    for ($y=0; $y<count($resultArray[$x]); $y++) {
                echo "<td>".$resultArray[$x][$y]."</td>";
        }
        echo "</tr>";
} 
?>

Open in new window

0
 

Author Comment

by:lghaman123
ID: 21823582
Thanks guys, I appreciate the help.  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question