簡體   English   中英

空白行,同時將數據導出為php中的csv文件

[英]blank rows while exporting the data as csv file in php

我需要在應用程序中將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
        ) 
)

我想問題從這里開始

$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;

嘗試在您的代碼中進行此更改

感謝user2063756,您為我指明了正確的方向。 我的CSV文件從第4行開始。 僅僅是因為我使用了包含我的常量的PHP包含文件。...並且該文件在“

代替 ”

我在最上面的問題上有空白行。 我添加了ob_end_clean(); 在寫入文件之前,問題是res

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM