【问题标题】:PHP export to CSV file has first two rows emptyPHP 导出到 CSV 文件的前两行为空
【发布时间】:2015-10-27 02:37:16
【问题描述】:

我在一个数组中有以下数据,并想写入 CSV 文件。一切正常,除了我在 .csv 文件中看到前两个空行

感谢任何帮助...再次感谢!!!!

CSV 文件中的输出如下所示 空 Line1
空 Line2
ip 地址 ip 状态描述 hostmane mac owner 设备备注 数据……

数组数据:

Array (
[0] =>Array ( [ip address] => 1.3.2.1 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => ) 
[1] =>Array ( [ip address] => 1.3.2.2 [ip state] => Reserved [description] => [hostname] => linux [mac] => [owner] => test [device] => Linux Server [note] => Linux123 )
[2] => Array ( [ip address] => 1.3.2.3 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
[3] => Array ( [ip address] => 1.3.2.4 [ip state] => Active [description] => [hostname] => [mac] => [owner] => [device] => [note] => )
)  

我用来写入 CSV 文件的 PHP 函数是:

switch("export-to-csv")
{   
    case "export-to-csv" :
        // Submission from
        $filename = $_GET["exformat"] . ".csv";
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Content-type: text/csv");
        header("Content-Disposition: attachment; filename=\"$filename\"");
        header("Expires: 0");
        ExportCSVFile($data);
        //$_GET["exformat"] = '';
        exit();
    default :
        die("Unknown action : ".$_POST["action"]);
        break;
}

function ExportCSVFile($records) {
    // create a file pointer connected to the output stream 
    #print_r ($records); return;
    $fh = fopen( 'php://output', 'w' );
    $heading = false;
    if(!empty($records)) {
        foreach($records as $row) {            
            if(!$heading)  {
              // output the column headings
              fputcsv($fh, array_keys($row));
              $heading = true;
            }
        // loop over the rows, outputting them
         fputcsv($fh, array_values($row));              
      }       

    }
}

【问题讨论】:

  • 您也可以在if(!$heading) 之后添加一个 else 条件来检查空字符串 ('')。我建议修剪字符串以捕获只有空格的条目。
  • 感谢 devlin 的快速回复。我在 if(!$heading) $row = trim(preg_replace('/\s+/', ' ', $row)); 之前尝试使用此行但没有帮助。
  • 任何人都知道如何避免 .csv 文件下的前两个空行...
  • 尝试添加 ob_clean();在 fputcsv 之前。希望这会有所帮助。

标签: php


【解决方案1】:

请任何文件,如框架的 index.php 或任何其他开头有空行的文件,这可能会导致 csv 中的空行。

【讨论】:

    【解决方案2】:

    请检查您在函数调用之前包含的其他文件以及哪些文件有打印空间等。您可以检查使用记事本在记事本中打开导出的 csv 文件,您可以查看是否有空间或您的 php 代码是否正常工作完美,没有任何问题。我已经用波纹管阵列测试了它的工作正常。

    $data = array(
        '0' => array('ip address' => '1.3.2.1', 'ip state' => 'Active'),
        '1' => array('ip address' => '1.3.2.2', 'ip state' => 'Reserved'),
        '2' => array('ip address' => '1.3.2.3', 'ip state' => 'Active'),
        '3' => array('ip address' => '1.3.2.4', 'ip state' => 'Active')
    );
    
    switch ("export-to-csv")
    {
        case "export-to-csv" :
            $filename = "1.csv";
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Content-type: text/csv");
            header("Content-Disposition: attachment; filename=\"$filename\"");
            header("Expires: 0");
            ExportCSVFile($data);
            exit();
        default :
            die("Unknown action : " . $_POST["action"]);
            break;
    }
    
    function ExportCSVFile($records)
    {
        $fh = fopen('php://output', 'w');
        $heading = false;
        if (!empty($records))
        {
            foreach ($records as $row)
            {
                if (!$heading)
                {
                    fputcsv($fh, array_keys($row));
                    $heading = true;
                }
                fputcsv($fh, array_values($row));
            }
        }
    }
    

    【讨论】:

    • 感谢 Ravneet 和 Mitul 的回复。这对我检查我的 index.php 来说是一个很好的指针。与此同时,一个快速的问题是,如果我正在写入 CSV 文件的数组没有任何空格,那么为什么 fputcsv 函数会写入空格?我还检查了记事本中的输出,这也显示了两个新的空行。我还尝试在写入 CSV 文件之前检查数组中的空行,这也不起作用。有什么想法???
    • 当您使用 php 代码下载任何文件时,如果在此之前和之后有多余的空间或内容,它将自动在输出文件中添加内容
    猜你喜欢
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多