Public functionExport(){//data from the database, in order to save memory, do not read the data once to the memory, a row of a sentence to read the handle//Output Excel file header, you can replace the user.csv with the file name you wantHeader (' Content-type:application/vnd.ms-excel ');Header (' content-disposition:attachment;filename= ' order.csv "');Header (' cache-control:max-age=0 ');$where=Array("Paid"=1,"Pay_type"=Array("NEQ","Offline"),"Status"=Array("LT",3),);$stmt= M ("Group_order"),Field("Order_id,order_name,num,price,total_money,contact_name,phone,zipcode,adress,wx_cheap,balance_pay,payment _money,tuan_type,pay_time,pay_type,third_id,is_mobile_pay,paid,status "),where($where),Order("order_id DESC"),Limit( +),Select();//Open PHP file handle, php://output indicates direct output to browser$fp= fopen (' Php://output ', ' A ');//Output Excel column name information$head= Array("Order Number","Order name","Purchase Quantity","Price","Total Price","Contact person name","Contact Phone","Postcode","Detailed Address","Preferential Amount","Balance Payment amount","Actual payment amount","Sale Type (2 is in kind)","Payment Time","Payment Type","third-party payment ID","Whether it is mobile payment","Whether to pay","Order Status");foreach($head as$i=$v) {//CSV Excel support GBK encoding, be sure to convert otherwise garbled$head[$i] = Iconv (' Utf-8 ', ' GBK ', $v);}//Writes data to a file handle via FputcsvFputcsv ($fp, $head);//Counter$cnt= 0;//Every $limit line, refresh the output buffer, not too big, not too small$limit= -;//row-by-line extraction of data without wasting memory$count= count ($stmt); for($t=0;$t<$count;$t++) {$cnt++;off($limit== $cnt) { //Refresh the output buffer to prevent problems caused by excessive dataOb_flush ();Flush ();$cnt= 0;}$row= $stmt[$t];foreach($row as$i=$v) {if($i==' Pay_time '){$v=date ("Y-m-d,h:i:s",$v);}$row[$i] = Iconv (' Utf-8 ', ' GBK ', $v);}Fputcsv ($fp, $row);}fclose ($fp);}
The above describes the export from the database to the CSV file method, including the content of the CSV file, I hope to be interested in PHP tutorial friends helpful.