This article mainly introduces the detailed explanation of a few lines of PHP code to achieve CSV format file output. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor to take a look.
Organize the documents, search out a php to implement CSV format file output, and organize and streamline it a little for sharing.
//适用于不需要设置格式简单将数据导出的程序,多多指教...... $str .= 'pro_code'.','.'words'.'\n';//首先写入表格标题栏 foreach($is_error as $key => $value){//循环写入数据 $str .= $value['pro_code'].",".$value['words']."\n"; } $str = iconv('utf-8','gb2312',$str);//防止中文乱码 $filename = "./output.csv";//文件路径及名字 export_csv($filename,$str); //导出 //自定义输出函数 function export_csv($filename,$str){ header("Content-type:text/csv"); header("Content-Disposition:attachment;filename=".$filename); header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); header('Expires:0'); header('Pragma:public'); echo $str; }
The above is the detailed content of Detailed explanation of examples of CSV format file output in PHP. For more information, please follow other related articles on the PHP Chinese website!