slipster70
06-22-2006, 08:17 PM
Hello.
I am trying to output the results of a query to an Excel spreadsheet.
All I get in Excel is the top line of headers. No data below. And all the column headers get put together into one cell!
Thanks for any and all help.
slipster70
code:
// build query
$query = "SELECT * FROM trouble_ticket";
// execute query
$result = mysql_query($query);
$fp = fopen("./dump.sql", "a");
while($row = mysql_fetch_array($result)) {
fwrite($fp, "insert into trouble_ticket ".
"values ('$row[col1]', '$row[col2]')\n");
}
fclose($fp);
// Reading the data back in
$arr = file("./dump.sql");
for($i=0; $i<sizeof($arr); $i++)
{
mysql_query($arr[$i]);
// execute each line as SQL statement
}
// output as CSV
$csv_output = '"Column1", "Column2"';
$csv_output .= "\015\012";
while($row = mysql_fetch_array($result))
{
$csv_output .= '"'.$row[col1].'", "'.$row[col2].'"';
$csv_output .= "\015\012";
}
header("Content-type: application/vnd.ms-excel");
// attachment disposition will force prompt to save to filename
header("Content-disposition: attachment; filename=document_" . date("Y-m-d") . ".xls");
print $csv_output;
exit;
?>
I am trying to output the results of a query to an Excel spreadsheet.
All I get in Excel is the top line of headers. No data below. And all the column headers get put together into one cell!
Thanks for any and all help.
slipster70
code:
// build query
$query = "SELECT * FROM trouble_ticket";
// execute query
$result = mysql_query($query);
$fp = fopen("./dump.sql", "a");
while($row = mysql_fetch_array($result)) {
fwrite($fp, "insert into trouble_ticket ".
"values ('$row[col1]', '$row[col2]')\n");
}
fclose($fp);
// Reading the data back in
$arr = file("./dump.sql");
for($i=0; $i<sizeof($arr); $i++)
{
mysql_query($arr[$i]);
// execute each line as SQL statement
}
// output as CSV
$csv_output = '"Column1", "Column2"';
$csv_output .= "\015\012";
while($row = mysql_fetch_array($result))
{
$csv_output .= '"'.$row[col1].'", "'.$row[col2].'"';
$csv_output .= "\015\012";
}
header("Content-type: application/vnd.ms-excel");
// attachment disposition will force prompt to save to filename
header("Content-disposition: attachment; filename=document_" . date("Y-m-d") . ".xls");
print $csv_output;
exit;
?>