Home  >  Article  >  Backend Development  >  Php implements csv writing and downloading

Php implements csv writing and downloading

不言
不言Original
2018-04-04 14:07:571388browse

The content of this article is the code to implement CSV writing and downloading in PHP. Now I share it with everyone. Friends in need can also refer to the content of this article.

$title1 = 'Year and Month\Details , Monthly loan amount, monthly profit amount, monthly theoretical payment amount, monthly actual payment amount, monthly unpaid amount, total monthly customers'."\n";
header("Cache- Control: public");
header("Pragma: public");
header("Content-type:application/vnd.ms-excel");
      $filename = $title;
      header("Content-Disposition:attachment;filename=$filename.csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();

//Linux system encoding conversion
//$header_str = mb_convert_encoding ($title1,"gbk","utf-8");

//$csv_line_data = mb_convert_encoding ($csv_line_data,"gbk", "utf-8");

//windwos encoding conversion

                                                                                                                                                                 ​ Removed,replaced with gbk

which supports more characters //

$data = iconv("utf-8","gb2312//IGNORE ",$data);

                                                                                                                                                                                                                                                                                        ​##$csv_line_data = iconv("utf-8","gbk//IGNORE",$csv_line_data);

//$header_str = iconv("utf-8", 'gbk', $title);
//$csv_line_data = iconv("utf-8", 'gbk//IGNORE', $csv_line_data);
ob_end_clean();

//Write out

echo $header_str;

echo $csv_line_data;Related recommendations:
A few lines of php code to implement CSV format file output case

Detailed example of exporting PHP from database to .csv file

The above is the detailed content of Php implements csv writing and downloading. 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