MySql-Excel - only dates export..no strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

MySql-Excel - only dates export..no strings

Post by kbrown3074 »

I created a php page to create a excel sheet that puts data into the sheet by exporting the html table codes and such. Any data that has a date value exports fine, but anything that is a string will not export. I am using a 'foreach' loop from a 'mysql_fetch_row' variable. The print line is: print("<td>$dbrow[$col_num]</td>")

Is there a way to modify the print line to send the info as a string, or do I have to set up some kind of excel formatting as I create the sheet?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you post your actual code so we can see what is happening?
User avatar
kbrown3074
Forum Contributor
Posts: 119
Joined: Thu Jul 20, 2006 1:36 pm

Post by kbrown3074 »

Just a quick background..the dbase and the tables do exist so that part is not a problem. The spreadsheet opens fine under excel on a windows box BUT the html code results display in openoffice. This will be used mainly by a windows user so that is secondary at this point.

<?
$dbase=$_GET['dbase'];
$edt1=$_GET['edt1'];
$edt2=$_GET['edt2'];
$filename=$_GET['filename'];
$tsrnum=$_GET['tsrnum'];
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");

$pagehead = "AGENT " . $tsrnum . " REPORT";
?>
<strong>Agent State Report</strong>
<table border="1">
<tr>
<td bgcolor="#FFFFCC"></td>
<?
$dbcon = mysql_connect("localhost", "test", "test");
mysql_select_db($dbase,$dbcon);
print("<table border=1>\n");
print("<tr>\n");
print("<td bgcolor=#FFFFCC></td>\n");

$export="select * from $edt2";

$expresult = mysql_query($export,$dbcon) or die (mysql_error());
$num_fields = mysql_num_fields($expresult);

while($rowx = mysql_fetch_field($expresult))
{
print("<td bgcolor=\"#FFFFCC\">");
print("$rowx->name");
print("</td>");
}
print("</tr>\n");

while($dbrow = mysql_fetch_row($expresult))
{
print("<tr>");
$col_num = 0;
foreach($dbrow as $key=>$value)
{
if($dbrow[$col_num] > 0)
{
print("<td>$dbrow[$col_num]</td>");
}
else
{
print("<td> </td>");
}
$col_num++;
}
print("</tr>\n");
}
print("</table>\n");
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Please use bbCode (

Code: Select all

 or [ php ]) tags when posting code.
[quote="kbrown3074"]Just a quick background..the dbase and the tables do exist so that part is not a problem.  The spreadsheet opens fine under excel on a windows box BUT the html code results display in openoffice.  This will be used mainly by a windows user so that is secondary at this point.

[syntax=php]<?php
        $dbase=$_GET['dbase'];
        $edt1=$_GET['edt1'];
        $edt2=$_GET['edt2'];
        $filename=$_GET['filename'];
        $tsrnum=$_GET['tsrnum'];
        header("Content-Type:  application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$filename.csv");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Expires: 0");

        $pagehead = "AGENT " . $tsrnum . " REPORT";
?>
        <strong>Agent State Report</strong>
        <table border="1">
        <tr>
        <td bgcolor="#FFFFCC"></td>
<?php
        $dbcon = mysql_connect("localhost", "test", "test");
        mysql_select_db($dbase,$dbcon);
        print("<table border=1>\n");
        print("<tr>\n");
        print("<td bgcolor=#FFFFCC></td>\n");

        $export="select * from $edt2";

        $expresult = mysql_query($export,$dbcon) or die (mysql_error());
        $num_fields = mysql_num_fields($expresult);

        while($rowx = mysql_fetch_field($expresult))
        {
                print("<td bgcolor="#FFFFCC">");
                print("$rowx->name");
                print("</td>");
        }
        print("</tr>\n");

        while($dbrow = mysql_fetch_row($expresult))
        {
           print("<tr>");
           $col_num = 0;
           foreach($dbrow as $key=>$value)
           {
                if($dbrow[$col_num] > 0)
                {
                   print("<td>$dbrow[$col_num]</td>");
                }
                else
                {
                   print("<td> </td>");
                }
                $col_num++;
           }
           print("</tr>\n");
        }
        print("</table>\n");
?>
[/quote]
Post Reply