简体   繁体   English

将逗号作为分隔符导出到php中的CSV

[英]put comma as separator in export to CSV in php

I have to get mysql data in a CSV file on click of download button. 我必须点击下载按钮在CSV文件中获取mysql数据。 The below code exports data successfully but adds space as separator. 以下代码成功导出数据,但添加空格作为分隔符。 But I need comma to be separator not space, As there are space in mysql data and also on static header of csv file. 但我需要逗号分隔符而不是空格,因为在mysql数据中还有空格,也在csv文件的静态头上。

HTML code is below. HTML代码如下。

<form name="downloadform" id="downloadform" action="exportcsv.php" method="POST"> 
<div style="font-weight:bold; line-height:30px;" class="TableContainer">
        <label>Download CSV Format:</label>
        <button class="blue" name="btnDwld" id="btnDwld" type="submit">Download</button>
</div>
</form>

And export to csv code is below. 并导出到csv代码如下。

<?php 
require_once 'dbconfig.php';
if(isset($_POST['btnDwld']))
{
    $filname = "UploadRateWeight-".date("d-M-Y-H:i:s");
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=$filname.csv");
header("Pragma: no-cache");

    function echocsv($fields)
{
    $separator = ',';
        foreach ($fields as $field) {
           if (preg_match('/\\r|\\n|,|"/', $field)) {
                   $field = '"' . str_replace('"', '""', $field) . '"';
              } // end if
           echo $field.$separator;
        }   // end foreach
    echo "\r\n";
}

    $content = '';
    $title = '';
    $title = array('0'=>'head part1','1'=>'head part2','2'=>'head part3','3'=>'head part4','4'=>'head part5');
    $headerr = echocsv($title);

    $sql = "SELECT * FROM table_name";
    $query = mysql_query($sql);
    while($rs = mysql_fetch_array($query)) {
        $name = $rs["name"];
        $line = array('0'=>$name,'1'=>'','2'=>'','3'=>'','4'=>'');
        echocsv($line);
        //$content .= "\n";
    }
}

The out put in a .CSV file looks like below. 输出.CSV文件如下所示。

head
----------
name1
name2
name3

As you can see I have put 1st colum name as 'head part1' but it will show like 'head' as its taking space as separator. 正如你所看到的,我已将第一个列名称作为“head part1”,但它将显示为“head”作为其占用空间作为分隔符。

So second column name takes this. 所以第二列名称就是这个。

part1,head
----------

and third column name takes this. 第三列名称取此。

part2,head
----------

and so on. 等等。 So how to use a comma to be separator while exporting CSV file in php ?? 那么在php中导出CSV文件时如何使用逗号分隔?

Don't write csv file's manually. 不要手动编写csv文件。 Use the built in function. 使用内置功能。

eg http://uk3.php.net/manual/en/function.fputcsv.php 例如http://uk3.php.net/manual/en/function.fputcsv.php

<?php

$delimiter = ',';
$enclosure = '"';

$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

$fp = fopen('file.csv', 'w');

foreach ($list as $fields) {
    fputcsv($fp, $fields, $delimiter, $enclosure);
}

fclose($fp);
?>

The above code will create a file called file.csv with your data on it. 上面的代码将创建一个名为file.csv的文件, file.csv包含您的数据。 If you then want to send this file to the user as a CSV file, you can do this: 如果您希望将此文件作为CSV文件发送给用户,则可以执行以下操作:

<?php

// Send the generated csv file to the browser as a download
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
readfile('file.csv');

?>

Alternatively, you can send the CSV file to download directly without creating the file on server like this: 或者,您可以直接发送CSV文件进行下载而无需在服务器上创建文件,如下所示:

<?php

// mini-config
$delimiter = ',';

// Your data
$list = array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);

// Send headers
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");

// Output csv data
foreach ($list as $row) {
    echo implode($delimiter, $row) . "\r\n";
}

?>

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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