Hi: I use a php script to export table data to excel. Excel 2007 says the file format is different specified by the file extension... <?php $dbhost = "localhost"; // Usually localhost. $dbname = "testdb"; // Database name. $dbuser = "root"; // Database username. $dbpass = "pass"; // Database password. $dataname = "web-data"; // FILENAME PREFIX THAT WILL BE ADDED TO XL FILE. $tablename = "user"; // name of table to extract data from. $fields = "id, username, password, regi_time"; // the fields in the table you want. seperated with comma. Example ($fields = "field1, field2, field3";) // ////////////////////////////////////////////////// /////////// DO NOT EDIT THIS PART OF FILE //////// ////////////////////////////////////////////////// $datestamp = date("d-m-Y"); $dbh=mysql_connect ($dbhost, $dbuser, $dbpass) or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ($dbname); $query = ("SELECT $fields FROM $tablename"); $result=mysql_query($query) or die('Error, unsuccesseful when querying database'); $myarray = array(); while($row = mysql_fetch_array($result, MYSQL_NUM)) { $myarray[] = implode("\t", $row); } $myarray = implode("\r\n", $myarray); $filename = "$dataname-$datestamp.xls"; header("Content-Disposition: attachment; filename=$filename"); header("Content-Type: application/vnd.ms-excel"); echo $myarray; ?> PHP:
It looks like you're creating the file in tab delimited format, yet naming it "*.xls"? Try naming it "*.txt" instead.
All your data is in one column because you used tabs to delimit your data and called it csv (Comma seperated values). If you use commas instead of tabs, it'll go into different columns. However, if you call it *.txt, instead of *.csv, Excel will treat it as tab delimited and will put your data in seperate columns.
* thx * Almost there. Timestamp can be displayed correctly in .txt, but not .csv.... It looks like ######
just expand the width of the column with the "#####" data to be able to view it properly. The correct data is in there, it's just that excel doesnt automatically resize column widths.