【问题标题】:blank rows while exporting the data as csv file in php在php中将数据导出为csv文件时出现空白行
【发布时间】:2013-08-22 11:11:40
【问题描述】:

我需要在我的应用程序中将 db 内容导出为 csv 文件。它正在工作。但是内容不是从第一行开始的。它从第 40 行开始。我还需要做什么?

header('Content-Encoding: UTF-8');
header("Cache-Control: cache, must-revalidate");
header("Pragma: public");
header('Content-Type: text/csv, charset=UTF-8; encoding=UTF-8');
$filename = date('Y_m_d_s');
header("Content-Disposition: attachment; filename=\"".$filename.".csv\"");

if($dbResult) {
    $cnthead=0; $cntr =count($dbResult[0])-1;
    foreach($dbResult[0] as $key => $Val){ 
        echo $key;
        if($cnthead < $cntr) {
            echo ",";
            $cnthead++;
        }
    }
    echo "\n";
    foreach($dbResult as $dbRes){
        $cntcontnt=0;
        foreach($dbRes as $dbVal){ 
            echo $dbVal; 
            if($cntcontnt < $cntr) {
                echo ",";
                $cntcontnt++;
            }
        }
        echo "\n";
    }
}

$dbResult 数组如下,

Array
(
    [0] => Array
        (
            [Appraisee Name] => aaa R
            [Team] => Software
            [Appraisee Emp No] => -
            [Appraiser Name] => vvv A
            [Appraiser Emp No] => -
            [Reviewer Name] => sss S
            [Reviewer Emp No] => -
            [Current Appraisal Status] => start
        )

    [1] => Array
        (
            [Appraisee Name] => gan R
            [Team] => Software
            [Appraisee Emp No] => -
            [Appraiser Name] => fcv R
            [Appraiser Emp No] => -
            [Reviewer Name] => sss S
            [Reviewer Emp No] => -
            [Current Appraisal Status] => start
        ) 
)

【问题讨论】:

  • 在处理 csv 之前检查您的查询结果。
  • 你能粘贴 $dbResult 值供我参考吗
  • 我猜问题是 $cntr =count($dbResult[0])-1;它给出了您的第一条记录的计数。如果您需要所有结果,假设您需要将其更改为您的结果计数。或者您需要为任何特定的代码提供此代码?

标签: php mysql csv export


【解决方案1】:

我在最上面的问题上有空行。 我添加了 ob_end_clean(); 在写入文件之前,问题是 res

【讨论】:

    【解决方案2】:

    我猜问题从这里开始

    $cntr =count($dbResult[0])-1; //so $cntr = 7;

    foreach($dbResult[0] as $key => $Val){ 
        echo $key;
        if($cnthead < $cntr) {//this condition only true for first 7 loops after it will fails
            echo ",";
            $cnthead++;
        }
    }
    

    例如,$dbResult 有超过 7 条记录,它不会进入第一个 forloop if 语句。如果你想循环 $dbResult 所有记录然后更改你的代码如下。

    我猜你需要改变

    $cntr =count($dbResult[0])-1;
    

    进入

    $cntr =count($dbResult)-1;
    

    在您的代码中尝试此更改

    【讨论】:

    • foreach 语句运行顺利。在 CSV 中的内容之前,还显示了一些空白行。如何避免 CSV 导出中的空间。这是我的问题。
    • 我猜这是你的 csv 数据 $dbVal since if($dbVal != '') {echo $dbVal; if($cntcontnt
    • 你能把你的csv发给我吗?
    • 嗨@sudhakar,我解决了这个问题。在我的 php 文件中,我空白地留下了 2 行。因此,CSV 中仅显示空白行。感谢您宝贵的时间。
    【解决方案3】:

    感谢 user2063756,您为我指明了正确的方向。 我的 CSV 文件从第 4 行开始。 仅仅因为我使用了我的常量的 PHP 包含文件....并且该文件在 " 之前有一个空行

    曾经 "

    而不是 "

    【讨论】:

      猜你喜欢
      • 2013-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 2020-02-25
      • 1970-01-01
      相关资源
      最近更新 更多