PhP导出.csv将分隔符从逗号更改为其他人

时间:2015-12-08 19:58:22

标签: php csv

我有这个PhP代码,用户点击一个按钮,它会自动下载.csv文件,里面有不同类型的信息。所以这个.csv文件是用逗号分隔的,这意味着只要有逗号,指针就会转到下一个单元格。但我想将分隔符更改为除逗号之外的任何内容,因为我的一些数据有逗号,例如Location列有501A,501B等数据,它需要在单个单元格上。但是它会转到另一个单元格,因此当我以纵向打印时,id不适合。所以我只想问我如何更改分隔符并用逗号维护数据?谢谢!

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "envoy");
function db_connect() {

  $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die("ERROR!");
  mysqli_select_db($db, DB_NAME);

  return $db;
}

$db = db_connect();
date_default_timezone_set("EST5EDT");
$date = date('m-d-Y,h.i.sa');
$date2 = date('m/d/Y');
$filename = $date.'.csv';
//$fp = fopen($filename,"w");

$sql2 = "SELECT sku as Sku,name as 'Brochure Name',location as Location FROM brochureinfo where modified LIKE '$date2' ORDER BY name  ";
$rs2 = mysqli_query($db, $sql2);
$total_records = mysqli_num_rows($rs2);
mysqli_close($db);

if($total_records>0){
$row = mysqli_fetch_assoc($rs2);

$seperator = "";
$comma = "";

foreach ($row as $name => $value){
    $seperator.= $comma. ''.str_replace('','""',$name);
    $comma=",";

}
$seperator .= "\n";

//echo $seperator;




mysqli_data_seek($rs2,0);

while ($row = mysqli_fetch_assoc($rs2)){


//$seperator = "";
$comma = "";

foreach ($row as $name => $value){

            $value = str_replace( array( "\r" , "\n", "\r\n", "\n\r" ) ,'' , $value);
            $value = str_replace('</b><br>','',$value);
            $value = str_replace('<b>','',$value);
            $value = str_replace('<br>','',$value);
            $value = str_replace('<br />','',$value);

    $seperator.= $comma. ''.str_replace('','""',$value);
    $comma=",";

    }
$seperator .= "\n";
}

header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');

echo $seperator;
}
else{
    header('Content-Type: text/csv');
header("Content-Disposition: attachment; filename=$date.csv");
header('Pragma: no-cache');
echo "No record modified today";}

2 个答案:

答案 0 :(得分:6)

您可以将fputcsv()用于csv格式的生成数据

int fputcsv ( resource $handle , array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape_char = "\" ]]] )

修改

您的代码看起来会像这样:

<?php
define("DB_HOST", "localhost");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_NAME", "envoy");
function db_connect() {

  $db = mysqli_connect(DB_HOST, DB_USER, DB_PASS) or die("ERROR!");
  mysqli_select_db($db, DB_NAME);

  return $db;
}

$db = db_connect();
date_default_timezone_set("EST5EDT");
$date = date('m-d-Y,h.i.sa');
$date2 = date('m/d/Y');
$filename = $date.'.csv';
$fp = fopen($filename,"w");

$sql2 = "SELECT sku as Sku,name as 'Brochure Name',location as Location FROM brochureinfo where modified LIKE '$date2' ORDER BY name  ";
$rs2 = mysqli_query($db, $sql2);
$total_records = mysqli_num_rows($rs2);
mysqli_close($db);

if ($total_records>0) {
    $row = mysqli_fetch_assoc($rs2);

    $delimiter = ';';
    $data = [];
    foreach ($row as $name => $value){
        $data[] = $name;
    }
    fputcsv($fp, $data, $delimiter);


    mysqli_data_seek($rs2,0);

    while ($row = mysqli_fetch_assoc($rs2)){
        fputcsv($fp, $row, $delimiter);
    }

    fclose($fp);

    header('Content-Type: text/csv');
    header("Content-Disposition: attachment; filename=$date.csv");
    header('Pragma: no-cache');

    echo $seperator;
}
else {
    fclose($fp);

    header('Content-Type: text/csv');
    header("Content-Disposition: attachment; filename=$date.csv");
    header('Pragma: no-cache');
    echo "No record modified today";
}

答案 1 :(得分:0)

使用PHP函数,fputcsv将每一行放在一个数组中(每个列值将成为数组的一个项目),因此您不必担心里面的逗号。由于您是直接下载文件,因此请使用PHP输出作为文件句柄。请记住在输出开始之前设置标题。

$output = fopen('php://output', 'w');

// output the column headings/ values
fputcsv($output, array('Column 1', 'Column 2', 'Column 3');