首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP导出到CSV文件的前两行为空

PHP导出到CSV文件的前两行为空
EN

Stack Overflow用户
提问于 2015-10-27 10:37:17
回答 2查看 2.7K关注 0票数 1

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

任何帮助我们都很感激。再次感谢!

CSV文件中的输出看起来像空的Line1

代码语言:javascript
复制
 empty Line2
代码语言:javascript
复制
 ip address ip state description hostmane mac owner device notes     data......

数组数据:

代码语言:javascript
复制
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函数是:

代码语言:javascript
复制
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));              
      }       

    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-10-27 12:21:55

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

票数 0
EN

Stack Overflow用户

发布于 2015-10-27 13:29:00

请检查你的其他文件,这是包括在函数调用,并有打印空间等。你可以检查,使用记事本,打开输出的csv文件在记事本,你可以看到是否有空间,你的php代码是完美的,没有任何问题。我已经用波纹数组测试过了,它工作得很好。

代码语言:javascript
复制
$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));
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33359067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档