$data = [ [标题1,标题2],[内容1,内容2],[内容1,内容2]];$fp=fopen('php://memory','w+'); // open up write to memoryforeach($data as $row){ $convertedRow=array(); foreach($row as $val){ $convertedRow[]=iconv('utf-8','gbk',$val)."\t";//防止中文乱码,防止打开文件时显示科学计算法(只针对长数字字符串有效) } fputcsv($fp,$convertedRow);}rewind($fp);$csvFile=stream_get_contents($fp);fclose($fp);ob_clean();$fileName = 'card_port_'.date('YmdHis').'.csv';header('Content-Type: text/csv; charset=utf-8');header("Content-Transfer-Encoding: binary ");header("Content-Type: application/force-download");header('Content-Length: '.strlen($csvFile));header('Content-Disposition: attachment; filename="'.$fileName.'"');echo ($csvFile);你也可以不写文件,直接把内容用,号隔开,每一行就用\n换行就可以
foreach($data as $row){ $convertedRow=array(); foreach($row as $val){ $convertedRow[]=iconv('utf-8','gbk',$val)."\t"; } $csvFile.=implode(',',$convertedRow)."\n";}
来源:https://www.cnblogs.com/fwqblogs/p/11078116.html