Here is the code I am using:
header("Content-Type: application/vnd.ms-excel");
include("DBConnection.php");
$result = mysql_query($query_text, $db_link);
$fieldcounts = mysql_num_fields($result);
for($i = 0; $i < $fieldcounts; $i++) {
$fieldtype = mysql_fetch_field($result, $i);
echo "$fieldtype->name [$fieldtype->type]";
if ($i < ($fieldcounts-1)) {
echo "\t";
} else {
echo "\n";
}
while ($myrow = mysql_fetch_array($result)) {
for($i = 0; $i < $fieldcounts; $i++) {
$fieldname = mysql_field_name($result, $i);
if ($i < ($fieldcounts-1)){
echo $myrow[$fieldname] . "\t";
} else {
echo $myrow[$fieldname] . "\n";
}
}
}
}
This works, but is there a way to format the cells to text? Ther are part numbers in the database, some start with 0's. In the mysql database, the 0's are there, but when I export it to the excel spreadsheet, they are gone. Can I format the excel cells to text before putting the data in?