I’m using an Ajax Web Table to display the data shown in a database/table. When I export my file into an excel spreadsheet fields after a certain length appear as xxE+XX. So for example if I have the a field with the value 123456789 when exported i would get 12.34E+9.
Is there a way to get around this?
my code is as follows
else if(isset($_GET['export']))
{
session_start();
ob_start();
$this->mysqlConnect();
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=report.xls");
header("Pragma: no-cache");
header("Expires: 0");
$select = "SELECT * FROM customers";
$export = mysql_query($select);
$count = mysql_num_fields($export);
for ($i = 0; $i < $count; $i++) {
$header .= mysql_field_name($export, $i)."\t";
}
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);
if ($data == "") {
$data = "\n(0) Records Found!\n";
}
print "$header\n$data";
exit();
All help and feedback is greatly appreciated!
EDIT
phpMyAdmin has the same issue when selecting “csv” or “excel” format. But when Exporting as MS-Excel 2000 it solves the problem. Anyone know what the difference in exporting might be?
I haven’t tested this but I think you can prevent this by specifying the number as a string value. The
=sign should let excel know the value is a ‘formula’ and that it is a string like so:Using a string value is the only way I know of to get around the exponential notation as Excel prefers to shorten large numbers for display reasons. You can see that the actual value remains intact and can be viewed in the formula bar when a cell is selected even if the display shows an exponent.
Whether saving it as a string will break numerical formulas/functionality is something I can’t address. It doesn’t seem advisable to me to use a string simply to avoid the exponential display as it is just that: a display. The numeric value is still correct in your data.