Php implements csv writing and downloading

不言
Release: 2023-03-23 08:14:01
Original
1425 people have browsed it

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!