我需要在我的Application中将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
)
)
答案 0 :(得分:0)
我想问题从这里开始
$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条记录,如果你想循环$ dbResult所有记录,那么它将不会首先进入forloop。然后按以下方式更改你的代码。
我想你需要改变
$cntr =count($dbResult[0])-1;
进入
$cntr =count($dbResult)-1;
在代码中尝试此更改
答案 1 :(得分:0)
感谢用户2063756,您指出了我正确的方向。 我的CSV文件是从第4行开始的。 只是因为我使用的是我的常量的PHP包含文件....而且这个文件在&#34;之前有一个空行。
WAS &#34;
而不是 &#34;
答案 2 :(得分:0)
我在最上面的问题上有空白行。 我加了 ob_end_clean(); 写入文件之前,问题出在解决