I have the code below that exports my mysql data to excel. The two porblems I have are that the data starts after five blank rows in excel. Is there a way to change this?
Also, I have a date column in my data and excel shows this as ###### is there anyway to have it automatically format this column as a date?
<?php
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$Date_Valid."_noon_actuals.xls");
header("Pragma: no-cache");
header("Expires: 0");
define(db_host, "localhost");
define(db_user, "user");
define(db_pass, "pass");
define(db_link, mysql_connect(db_host,db_user,db_pass));
define(db_name, "ops_db");
mysql_select_db(db_name);
?>
<?php
$select = "SELECT * FROM Actual_Noon";
$export = mysql_query($select);
$fields = mysql_num_fields($export);
?>
<?php
for ($i = 0; $i < $fields; $i++) {
$header .= mysql_field_name($export, $i) . "\t";
}
?>
<?php
while($row = mysql_fetch_row($export)) {
$line = '';
foreach($row as $value) {
if ((!isset($value)) OR ($value == "")) {
$value = "\t";
} else {
$value = str_replace('"', '""', $value);
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
$data = str_replace("\r","",$data);
?>
<?php
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
?>
<?php
print "$header\n$data";
?>