473,394 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

MySQL export into CSV : number cells are not recognized as numbers

Hi,

I've got a PHP script that create a CSV file from a MySQL query. It outputs columns of data perfectly fine. The only problem is that Excel doesn't recognize the numbers as being "numbers" (that you can manipulate).

For example, if I have 2 cells with numbers 121.3 and 345.2 and I want to add them up, I will get an error. I've checked the formatting of my cells and made sure it's set to "Numbers", but still, the problem persists.

Here's my PHP script. Am I doing something wrong here ?

Thanks!

Expand|Select|Wrap|Line Numbers
  1. function WriteCsv($query) {
  2. include('databaseManager.php');
  3.  
  4.     $csv_terminated = "\n";
  5.     $csv_separator = ";";
  6.     $csv_enclosed = '"';
  7.     $csv_escaped = "\\";
  8.     $sql_query = $query;
  9.  
  10.     // Gets the data from the database
  11.     $result = DatabaseManager::ExecuteQuery($query);
  12.     $fields_cnt = mysql_num_fields($result);
  13.  
  14.  
  15.     $schema_insert = '';
  16.  
  17.     for ($i = 0; $i < $fields_cnt; $i++)
  18.     {
  19.         $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
  20.             stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
  21.         $schema_insert .= $l;
  22.         $schema_insert .= $csv_separator;
  23.     } // end for
  24.  
  25.     $out = trim(substr($schema_insert, 0, -1));
  26.     $out .= $csv_terminated;
  27.  
  28.     // Format the data
  29.     while ($row = mysql_fetch_array($result))
  30.     {
  31.         $schema_insert = '';
  32.         for ($j = 0; $j < $fields_cnt; $j++)
  33.         {
  34.             if ($row[$j] == '0' || $row[$j] != '')
  35.             {
  36.  
  37.                 if ($csv_enclosed == '')
  38.                 {
  39.                     $schema_insert .= $row[$j];
  40.                 } else
  41.                 {
  42.                     $schema_insert .= $csv_enclosed . 
  43.                     str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
  44.                 }
  45.             } else
  46.             {
  47.                 $schema_insert .= '';
  48.             }
  49.  
  50.             if ($j < $fields_cnt - 1)
  51.             {
  52.                 $schema_insert .= $csv_separator;
  53.             }
  54.         } // end for
  55.  
  56.         $out .= $schema_insert;
  57.         $out .= $csv_terminated;
  58.     } // end whilexport.csv
  59.  
  60.     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  61.     header("Content-Length: " . strlen($out));
  62.     // Output to browser with appropriate mime type, you choose ;)
  63.     header("Content-type: text/csv");
  64.     //header("Content-type: text/csv");
  65.     //header("Content-type: application/csv");
  66.     header("Content-Disposition: attachment; filename=rapports_ventes_".date('Y-m-d').".csv");
  67.     echo $out;
  68.     exit;
  69.  
  70. }
  71.  
  72.  
Dec 22 '10 #1
0 1295

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Merlin | last post by:
Hello all! I'll make it short and sweet... I have a database, it looks something like this: id data --- ------------------------- 0 Some Data 1...
1
by: Martin Herrman | last post by:
Hi all, I have installed MySQL on my Linux Mandrake 10 workstation; I didn't change any default values (except for the user-accounts). I used a CMS to create a website which I want to upload to...
1
by: Julien | last post by:
Hi, I'm actually trying to export a repeater to excel, everything seems ok but the problem is that the cells are not validated This means that for example date, it is left-centered instead of been...
1
by: aPRSComp | last post by:
CREATE TABLE `test`.`buildings` ( `building` VARCHAR NOT NULL DEFAULT '', `location` VARCHAR(45) NOT NULL DEFAULT '', PRIMARY KEY(`building`) ) ENGINE = InnoDB; MySQL Error Number 1064
4
by: Eager VBA Learner | last post by:
I am trying to write an Excel VBA macro that will find all cells in Column A that have numbers (as opposed to text) and delete the entire rows (with the numbers in Column A). As a start, I tried...
4
by: jbiggs via AccessMonster.com | last post by:
I am in the process of converting our backend from Access to mySQL. There are 21 tables and all of them except for one exported correctly. Our clients table is giving me problems and I suspect it...
1
by: Reggie | last post by:
Hi and TIA! I'm exporting a dataset/datgrid to excel but some text fields get converted to numbers in excel and I loose leading zero's. I've tried several methods with no success. If anyone has...
19
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
4
by: Iruka07 | last post by:
NOTE: I am using Excel VBA. Please help me program the following problem: A positive integer n is said to be a ROUND NUMBER if the binary representation of n has as many or more zeros as ones....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.