fputcsv在.csv开头插入空行 [英] fputcsv inserting empty row in beginning of .csv

查看:220
本文介绍了fputcsv在.csv开头插入空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php

$filename="backup_".date('m/d/Y', time()).".csv";

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');

$q=mysqli_query($conn,"SELECT * FROM visitordb");
$file = fopen('php://output','w');
if(mysqli_num_rows($q)>0) {
    while($res = $q->fetch_assoc()) {
        $datas = $res["sno"].','.$res["UDID"].','.$res["taggnumber"].','.$res["name"].','.$res["designation"].','.$res["company"].','.$res["email"];
        fputcsv($file, explode(',', $datas));
    }
} else {

}

fclose($file);
}

在ms excel中查看时,以上代码在.csv文件的开头生成空行. .csv文件还可以在页面中生成任何html代码.

The above code generates empty line in the beginning of .csv file when viewed in ms excel. One more thing the .csv file also generates any html code in the page.

推荐答案

如果删除标头指令,会发生什么? 您必须知道标题按照定义由新行结束. 我可以想象这是一个\ n vs \ r \ n冲突.

what happens if you remove the header directives? You must know that headers are by definition concluded by a new line. I could imagine that this is a \n vs \r\n conflict.

如果一切都失败了,请将整个输出放入缓冲区并稍后进行修剪:

If everything fails, get the whole output into buffer and trim it later:

ob_start();
...your code...
$out = ob_get_contents();
ob_end_clean();
echo trim($out);

最诚挚的问候

Zsolt

这篇关于fputcsv在.csv开头插入空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆