php导出excel乱码解决
// 输出Excel文件头,可把user.csv换成你要的文件名
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.ms-excel; charset=utf-8");//设置编码
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment;filename="'.date('Ymdhis').'.csv"');//导出文件名
header('Cache-Control: max-age=0');
// 打开PHP文件句柄,php://output 表示直接输出到浏览器
$fp = fopen('php://output', 'a');
$head = array ('用户昵称', '互动内容', '互动时间');
foreach ($head as $i => $v) {
// CSV的Excel支持GBK编码,一定要转换,否则乱码
$head[$i] = iconv('utf-8', 'gbk', $v);
}
// 将数据通过fputcsv写到文件句柄
fputcsv($fp, $head);
$data = array(1,2,3);
foreach ($data as $s1=>$s2){
$data[$s1] = iconv('utf-8', 'gbk', $s2);
}
fputcsv($fp, $v);