My web application allows user to select certain part of my MySQL table for export and the route I want to offer is either XLS file or CSV. Exporting and saving works just fine in both formats, but ... Excel does not want to show data correctly: instead of the value "441234567890" in one column it displays "4.41234E+11". The routes that I've already explored are:
1. Adding " ' " in front of the numbers to tell excel to treat them as text -- Excel treats info as text BUT displays the '-symbol and confuses users
2. Putting brackets around the value -- Excel ignores them and treats info as numbers and does the E+11 trick again
Is there a way to pass to excel column formats with the data -- number formats, column width, etc?
Or has someone found a way to tell Excel that values should be treated as text without changing information displayed in Excel?
Thanks for your help,
Oleg
PS ... here is the current code:
<?php
// Here we tell the browser that this is an excel file.
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=SMSRealty-Sales-Leads-".date("Y-m-d").".csv");
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Expires: 0");
//Variables to connect to MySQL database
error_reporting (E_ALL);
$user = "user"; //MySQL Username
$password = "password"; // MySQL Username's Password
$dbname= "dbname"; //Database Name
$tablename ="tablename"; // DB Table Name
$server="server"; //MySQL server name
$global_dbh = mysql_connect("localhost", $user, $password); // This establishes the database connection
mysql_select_db($dbname, $global_dbh); // Select what database to use
// You do not need to change the function
echo "Col1,Col2,Col3,Col4,Col5\n";
$query = mysql_query("SELECT col1, col2, col3, col4, col5, col6 FROM tablename WHERE col7='$variable' AND col8='Y' AND col8 BETWEEN '$startingtime' AND '$endtime'");
while($row = mysql_fetch_array($query)){
echo "$row[col1],$row[col2],$row[col3],$row[col4],$row[col5],$row[col6]\n";
}
?>