Hello!

I'm trying to extract a php table to an excel file. There's only ONE bit of problem. My table has alternating colors for each row and, lets say once extracted my cells range from A1:G20 in Excel, but instead of just coloring A:G, the row colors went all the way to the end! How can I fix this? I'm using some basic codes written below.

$resulttable = $_SESSION['resulttable'];
$dfw_data 	= $resulttable;
$dfw_filename 	= "test.xls";    

header("Content-type: application/msexcel");
header("Content-Disposition: attachment; filename=$dfw_filename");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$dfw_data";

Incase you want to see how my table looks like:

$Result = mysql_query($sql);

$NumFields = mysql_num_fields($Result);
$NumRows = mysql_num_rows($Result);

$table = "";

if ($NumRows==0)
{
 $table.= "<table>";
 $table.= "<tr><td>";
 $table.= "<font size='4'>";
 $table.= "<span style='color:red'>No Matches Found</span>";
 $table.= "</font>";
 $table.= "</td></tr>";
 $table.= "</table>";
$NumRows=1;
}
else
{

$table.= "<table border='1' cellpadding='3' style='border-collapse:collapse;'>"; 


    if(mysql_error())
    {
       $table.= "<tr><td>MySQL ERROR: " . mysql_error() . "</td></tr>";
    }
    else
    {


        $table.= "<tr style='background-color: #f3c685; color: #000000;'>";
        for ($i=0; $i < $NumFields; $i++)
        {     
            $table.= "<th>" . mysql_field_name($Result, $i) . "</th>"; 
        }
        echo "</tr>";

        //Loop thru results
        $RowCt = 0; //Row Counter
        while($Row = mysql_fetch_assoc($Result))
        {
            //Alternate colors for rows
            if($RowCt++ % 2 == 0) $Style = "background-color: #fdffca;";
            else $Style = "background-color: #fef1ab;";
            
           $table.= "<tr align='center' style='$Style'>";
            //Loop thru each field
            foreach($Row as $field => $value)
            {
                $table.= "<td>$value</td>";
            }
            $table.= "</tr>";
        }
        $table.= "<tr style='background-color: #f3c685; color: #000000;'><td colspan='$NumFields'>Query Returned " . mysql_num_rows($Result) . " records</td></tr>";
    }
    $table.= "</table>";



}
$_SESSION['resulttable'] = $table;

It's been hours, I believe I can bump this.

Amazing... Still no reply...

Generally, if you go two days without ANY kind of reply: Nobody who frequents the board knows how to do it. :P

I personally have never needed to export to excel and retain color...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.