Home > Backend Development > PHP Tutorial > Detailed explanation of examples of CSV format file output in PHP

Detailed explanation of examples of CSV format file output in PHP

零下一度
Release: 2023-03-12 11:18:01
Original
1683 people have browsed it

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;

}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template