troubleshooting Question

Formatting exported Excel spreadsheet from PHP

Avatar of Horalia Rodriguez
Horalia RodriguezFlag for United States of America asked on
6 Comments1 Solution345 ViewsLast Modified:
I have a few tables that can be exported to an Excel spreadsheet by using the following piece of code:

//Following is the place to decide to output as HTML or create the file instead.
//If requested parameter has been sent by the link to this php pave
//then it will set the headers so it will send it as "file" as in my sample
if (isset($_GET['xldownload']))
  //Also checking xldownload parameter value to make sure it is set correctly
  //Not necessary but good habit to expect a known value instead only checking for the existence
  if (intval($_GET['xldownload'])==1) {
    //removing the {DOWNLOADLINK}, because this will not be shown in the HTML page
    $data = str_replace("{DOWNLOADLINK}","",$data);
    header('Content-type: application/');
    header('Content-Disposition: attachment; filename=tabledata.xls');
  //Sending output as HTML to the client browser
  //So we need to put a link for download
  //We use the same script name however with xldownload=1 value (check out the href property of the anchor
  $data = str_replace("{DOWNLOADLINK}","<a href=\"sample.php?xldownload=1\">Download File</a>",$data);
  echo $data;

Many thanks to smozgur by the way, works great! But now I'm running into a tiny and almost insignifant problem, but I would like to correct it nevertheless... When I click on the link to export the table, the formatting is not kept. The table looks very nice in a browser, but it looks awful in Excel. I wouldn't like for whomever has to download the table to have the need to format the spreadsheet every time... Any ideas?
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros