Exporting Data to Excel
| From: | Ben C. | Date: | Thu, 23 Oct 2003 06:20:57 +0000 |
| Subject: | Exporting Data to Excel | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-167235@lists.php.net to get a copy of this message | ||
I am using the code below to export my data into an excel file. The code is
located in a password protected area which is checked against saved session
variables. However when I put session_start(); the download errors out.
Does any one have any suggestions? Please help.
<?
session_start();
$connection = @mysql_connect("$host", "$uname", "$pass") or
die("Couldn't
connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select
database.");
/* Specify the filename, which includes a datestamp for archival purposes...
*/
$date_stamp = date("Ymd");
$file_name = "ExportedData" . "_" . "$date_stamp" . ".xls";
/* Specify MIME type for tab-separated-values... */
//header("Content-type: text/tab-separated-values");
/* To open file directly into Excel, use this MIME type instead... */
header("Content-type: application/x-msexcel");
/* To force file download... */
header("Content-Disposition: attachment; filename=$file_name");
/* List the spreadsheet column labels (optional)... */
$column_labels = array("Label 1", "Label 2 ", "Label 3"); /* ...and
so on
*/
foreach($column_labels as $value) {
echo("$value\t");
}
echo("\n");
/* ...end column labels row */
/* Specify the SELECT query (you will need to change this)... */
$query = "SELECT * FROM buyers";
/* Execute the query... */
$result = mysql_query($query);
/* Format data as tab-separated values... */
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
while (list($key, $value) = each($row)) {
echo ("$value" . "\t");
}
echo ("\n");
}
?>