Home > Article > Backend Development > PHP data export case analysis
This article mainly introduces the case analysis of PHP data export. Interested friends can refer to it. I hope it will be helpful to everyone.
Introduction to php official documentation
<?php $list = array ( array('aaa', 'bbb', 'ccc', 'dddd'), array('123', '456', '789'), array('"aaa"', '"bbb"') ); $fp = fopen('file.csv', 'w'); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); ?>
Export complete example
<?php $name = 'test'; header ( "Content-type:application/vnd.ms-excel" ); header ( "Content-Disposition:filename=".$name.".csv" ); header ('Cache-Control: max-age=0'); //打开PHP文件句柄,php://output 表示直接输出到浏览器 $fp = fopen('php://output', 'a'); // 写入BOM头,防止乱码 fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // 生成的测试数据 function test() { for ($i=0; $i < 150000; $i++) { yield ['name', $i, '男']; } } // 表头 $headers = ['名字', '年龄', '性别']; fputcsv($fp, $headers); foreach (test() as $value) { fputcsv($fp, $value); } fclose($fp); ?>
Related recommendations:
Example of python exporting data to excel
Detailed explanation of how TP framework implements php data export to word
Export table data in js to excel file
The above is the detailed content of PHP data export case analysis. For more information, please follow other related articles on the PHP Chinese website!