0

I need to export all data from a MySql table to excel. I'm using the code below, which is working fine.

The problem is: there are 2 fields that are integers with more then 30 digits. So when I export it to the .csv file, excel transforms them into scientific notation.

Example: the field "95145612345641859415634194164163" transforms into "9.51456E+31"

How can I force it to read it as a string?

<?php


exportMysqlToCsv('export_csv.csv');



function exportMysqlToCsv($filename = 'export_csv.csv')
{

   $conn = dbConnection();

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $sql_query = "SELECT * FROM cadastros";


    $result = $conn->query($sql_query);

    $f = fopen('php://temp', 'wt');
    $first = true;
    while ($row = $result->fetch_assoc()) {
        if ($first) {
            fputcsv($f, array_keys($row));
            $first = false;
        }
        fputcsv($f, $row);
    } // end while

    $conn->close();

    $size = ftell($f);
    rewind($f);

    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Length: $size");
    header("Content-type: text/x-csv");
    header("Content-type: text/csv");
    header("Content-type: application/csv");
    header("Content-Disposition: attachment; filename=$filename");
    fpassthru($f);
    exit;

}

// db connection function
function dbConnection(){
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "creche_escolas";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    return $conn;
}
  • Possible duplicate of [How to show long numbers in Excel?](https://stackoverflow.com/questions/904519/how-to-show-long-numbers-in-excel) – Alberto Martinez Sep 14 '17 at 01:45
  • Append number with apostrophe(') . This this number is consider as string and you will get exact number that you want in excel. – Santosh D. Sep 14 '17 at 04:36

3 Answers3

0

You should make sure your number is a string when you insert a value, try:

fputcsv($f, ['12343231743986248756324354595', '123456', 12123437829564329758623456789]);

The first value will stay the same while the last is converted to 1.21234378296E+28

So you will need to replace the line fputcsv($f, $row); with a function to convert to strings such as number_format:

fputcsv($f, array_map(function($v){return number_format($v, 0, '', '');}, $row));
antoni
  • 5,001
  • 1
  • 35
  • 44
0

My logic working fine please use this while you export large number in excel First, make if the condition for you a large number and use commas " ' " with string

$large_number= "'".(string)$data->orders_id;

after export your excel file. replace your commas " ' " to " ' " again. your excel file cell will be ok with a large number

Thank you.

0

My logic working fine please use this while you export large number in excel First make if condition for you largenumber and use commas " ' " with string $large_number= " ' ".(string)$data->orders_id; after export your excel file . replace your commas " ' " to " ' " again. your excel file cell will be ok with large number thank you