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
$data = iconv("utf-8","gb2312//IGNORE ",$data);
//$header_str = iconv("utf-8", 'gbk', $title);
//$csv_line_data = iconv("utf-8", 'gbk//IGNORE', $csv_line_data);
ob_end_clean();
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!