Home>Article>Backend Development> Implementation code for php to export Excel files in csv format
The content of this article is about the implementation code of exporting Excel files in csv format through PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
index.php
exportToCsv($headerList,$data,$fileName,$tmp);
Export.php
$value) { $tmp[$key] = iconv("UTF-8", 'GBK', $value); } //使用fputcsv将数据写入文件句柄 fputcsv($fp, $tmp); //输出Excel列表名称信息 foreach ($headerList as $key => $value) { $headerList[$key] = iconv('UTF-8', 'GBK', $value);//CSV的EXCEL支持BGK编码,一定要转换,否则乱码 } //使用fputcsv将数据写入文件句柄 fputcsv($fp, $headerList); //计数器 $num = 0; //每隔$limit行,刷新一下输出buffer,不要太大亦不要太小 $limit = 100000; //逐行去除数据,不浪费内存 $count = count($data); for($i = 0 ; $i < $count ; $i++){ $num++; 、 //刷新一下输出buffer,防止由于数据过多造成问题 if($limit == $num){ ob_flush(); flush(); $num = 0; } $row = $data[$i]; foreach ($row as $key => $value) { $row[$key] = iconv('UTF-8', 'GBK', $value); } fputcsv($fp, $row); } } }
Link: https://pan.baidu.com/s/1e9BK6l5fY4aDDgYS7CLUig Password: v120
Related recommendations:
PHP How to implement fuzzy query (image and text code)
How to implement real-time editing of tables with php and ajax (code attached)
The above is the detailed content of Implementation code for php to export Excel files in csv format. For more information, please follow other related articles on the PHP Chinese website!