简体   繁体   中英

Exporting data from MySQL to Excel (csv) via PHP

I'm going to export data in SQL to Excel (csv) using PHP. I can export all data from SQL to Excel, but it is unknown Japanese language. For now, i can solved it; but other problem is about each row in SQL will export into one column in Excel.

Example (data in SQL)

ccode    country
US       United State
UK       United Kingdom
FR       France
KO       Korea
JP       東京

After export to Excel (csv)

    A                  
1   ccode,country
2   US,United State
3   UK,United Kingdom
4   FR,France
5   KO,Korea
6   JP,東京

Here is my code

<?php

header("Content-type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename=Export.csv');
//connection
$con = mysql_connect('localhost', 'root', '');
if(!$con){
    echo "Error connection";
}
//select db
$select_db = mysql_select_db('country', $con);
if(!$select_db){
    echo "Error to select database";
}
mysql_set_charset("utf8", $con);

//Mysql query to get records from datanbase
$user_query = mysql_query('SELECT * FROM countries');

//While loop to fetch the records
$contents = "ccode,country\n";
while($row = mysql_fetch_array($user_query))
{
    $contents.=$row['ccode'].",";
    $contents.=$row['country']."\n";
}

$contents_final = chr(255).chr(254).mb_convert_encoding($contents, "UTF-16LE","UTF-8");
print $contents_final;

?>

Here is what i want after export to Excel (csv)

    A          B 
1   ccode      country
2   US         United State
3   UK         United Kingdom
4   FR         France
5   KO         Korea
6   JP         東京

Can anyone help me to solve this problem? I'm appreciate to your help!

Thank in advance.

What you are doing is already correct. When importing the csv file in Excel, specify that the separator character is a comma. Alternatively, use a tab character as a separator in lieu of the comma.

You really should not be in the business of creating the CSV manually as you are doing. Consider using fputcsv() to do this:

//While loop to fetch the records
fputcsv('php://output', array('ccode','country'));
while($row = mysql_fetch_array($user_query, MYSQL_NUM)) {
    array_walk($row, function(&$item) {
          $item = mb_convert_encoding($item, "UTF-16LE","UTF-8");
    }
    fputcsv('php://output', $row); 
}

Note here that I am also doing the encoding step on each individual field value.

The benefit of using this method is that it will properly enclose all your fields as necessary to prevent certain values from breaking your CSV format. For example, if one of your database values contained , , your approach would break, whereas using a proper csv creation mechanism would handle this without a problem.

Another added benefit of this approach is that if you have a large set of data, this would use much less memory, in that you would only hold one line of data in memory at a time (before you output it and move to the next data row).

使用\\ t作为$ content变量以获取Excel工作表不同列中的数据

You will need to separate your columns with a ; rather than a , . The name 'comma-separated values' is a bit misleading here. So you would write:

while($row = mysql_fetch_array($user_query))
{
    $contents.=$row['ccode'].";";
    $contents.=$row['country']."\n";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM