Avatar of Lia Nungaray
Lia Nungaray
Flag for United States of America asked on

Formatting exported Excel spreadsheet from PHP

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/vnd.ms-excel');
    header('Content-Disposition: attachment; filename=tabledata.xls');
  }
}
else
{
  //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?
PHP

Avatar of undefined
Last Comment
Lia Nungaray

8/22/2022 - Mon
amit_g

If you are using any CSS to format the page, that might cause it. Change the CSS link to absolute or add a <base> in page's head section.

<link rel="STYLESHEET" href="../style/default.css" type="text/css">
to
<link rel="STYLESHEET" href="http://SomeDomain.com/style/default.css" type="text/css">

or add

<BASE href="http://SomeDomain.com/">
Lia Nungaray

ASKER
I'm actually placing the CSS directly into the php page:

<?php
include '../mysql_connect.php';
//PERIOD NAME
if (isset($_GET["periodname"]))
  $periodName = $_GET["periodname"];
//PERIOD START DATE
if (isset($_GET["pstartdate"]))
  $pstartDate = $_GET["pstartdate"];
//PERIOD END DATE
if (isset($_GET["penddate"]))
  $pendDate = $_GET["penddate"];
//Set counter to format table
$i=0;
//Set variable to count total Mondays - Saturdays
$total_weekdays=0;
//Set variable to count Sundays
$total_sun=0;

$data="
<html>
<head>
<style>
table.rpt {
      border-width: 1px;
      border-spacing: 1px;
      border-style: none;

etc.....
amit_g

What kind of formatting is lost? Could you show us it in work?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Lia Nungaray

ASKER
The formatting that is shown in the Excel spreadsheet differs only in color and in border thickness. I would like the cell's borders not to be so thick. Is there a way to change this Excel setting from PHP?
ASKER CERTIFIED SOLUTION
amit_g

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Lia Nungaray

ASKER
I ended up changing colors that were more close to the different color options Excel gives when formatting cells. Thanks anyways!