Home  >  Article  >  Backend Development  >  A few lines of php code to implement CSV format file output case

A few lines of php code to implement CSV format file output case

小云云
小云云Original
2018-02-10 11:13:521483browse

Organize the documents, search out a php to implement CSV format file output, and organize and streamline it a little for sharing. This article mainly introduces you to a few lines of php code to achieve CSV format file output. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.


//适用于不需要设置格式简单将数据导出的程序,多多指教......

$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;

}

Related recommendations:

log4Net Detailed examples of high-performance writing and CSV format

Perfect solution to the problem of garbled characters when php exports Excel's .csv format data

Convert each row of the HTML table to a CSV format array

The above is the detailed content of A few lines of php code to implement CSV format file output case. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn