如何使用php将长数字导出为csv文件中的字符串

问题描述 投票:1回答:1

当我尝试导出时,除NID字段外,所有其他字段都可以。这个数字的长度是17+,所以我怎么显示这个字段(nid)中的所有数字?我有

Here is the example of excel

$filename = "hello.csv";
            $query = "SELECT * FROM test_users";
            $result_users = $this->conn->prepare($query);
            $result_users->execute();
            // Create array
            $list = array ();

            // Append results to array
            array_push($list, array("## START OF May TABLE ##"));
            while ($row = $result_users->fetch(PDO::FETCH_ASSOC)) {
                array_push($list, array_values($row));
            }
            array_push($list, array("## END OF May TABLE ##"));

            // Output array into CSV file

            $fp = fopen('php://output', 'w');
            fputcsv($fp, ['Serial NO','English Name','Bangla Name','address','bn_address','user_no','nid','previous_cm','meter_no','type_of_user']);

            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename="'.$filename.'"');
            header("Content-Transfer-Encoding: binary");

            foreach ($list as $ferow) {
                fputcsv($fp, $ferow);
            }
php export-to-csv jackson-dataformat-csv
1个回答
0
投票

我遇到了相同的问题,并且已经按照以下方法解决了。

我在每个数组值中插入了单引号。这样,当您使用Excel打开CSV文件时,科学记号E + 15将不再显示。

// this function add a single quote for each member of array
function insertquote($value) {
    return "'$value'";
}

# here i send each value of array 
# to a insertquote function, and returns an array with new values, 
# with the single quote.

foreach ($list as $ferow) {
    fputcsv($fp, array_map(insertquote, $ferrow), ';');
}
© www.soinside.com 2019 - 2024. All rights reserved.