PHP generates a. csv file and downloads it to the browser

Source: Internet
Author: User
Tags money back

Recently, a project has been made to display the information of the order and to be able to put the relevant information into a. csv file and download it to the browser. First of all I want to explain is the. csv file, PHP has a special function to parse the type of files, related functions can be viewed on the official website. Note that the encoding format for the contents of the. csv file is in GBK format, so it is necessary to transcode the character format. The style of the file is as follows.

First, the header must be defined

               Output the header of the Excel file, you can change the user.csv to the filename you want (' Content-type:application/vnd.ms-excel '); header (' Content-disposition: Attachment;filename= "order data. csv" '); header (' cache-control:max-age=0 ');//open PHP file handle, php://output indicates direct output to browser $fp = fopen (' Php://output ', ' a ');//output Excel column name information

Second, the following is the head of the file

               $head = Array (' Order number ', ' ', ' Order name ', ' ', ' Business id ', ' ', ' Channel ID ', ' ', ' channel type ', ' ', ' product line name ', ' ', ' original order number ', ' ', ' Order Amount ', ' ', ' from value ', ' ', ' Order time ' , ', ' Payment contract number ', ', ' Channel name ', ' ', ' Payment contract number ', ' ', ' Supplier name ', ' ', ' operating platform ', ' ', ' product type ', ' ', ' ' Billing time ', ' ', ' channel cost ratio ', ' ', ' ' Channel cost ', ' ', ' receivables ', ', ' Settlement ratio ', ' ', ' payable settlement ', ' ', ' whether it has been returned ', ' ', ' whether it has been mentioned in batches ', ' ', ' remarks '); foreach ($head as $i + $v) {//CSV Excel supports GBK encoding, be sure to convert otherwise garbled $head [$ I] = iconv (' utf-8 ', ' GBK ', $v);} Writes data through Fputcsv to the file handle Fputcsv ($FP, $head);

Third, the following is the contents of the file, to my example is read from the database, do not understand it's okay, the principle is to isolate the results to put into the array, loop array, put each result into a new array and encode. I forgot to tell you that I'm using the CI (codeignite) framework

                     if ($this->input->get () = False) {//Gets data from the database, in order to save memory, do not read the data once to memory, a row of the clause can be read $sql = "Select charge_final.charge_id, Charge_final.product_name, charge_final.business_id, charge_final.channel_id, Char Ge_final.channel_type, Bass_productline.product_line_name, Charge_final.business_linkid, Charge_final.fee, Charge_ Final.msgfrom, Charge_final.charge_time, Charge_final.income_contract, Charge_final.income_channel_name , Charge_final.payment_contract, Charge_final.merchant_name, Charge_final.platform, CHARGE_FINAL.P Roduct_type, Charge_final.bill_time, Charge_final.channel_rate, Charge_final.channel_fee, (Charge_ Final.fee-charge_final.channel_fee) as account, Charge_final.payment_rate, Charge_final.payment_fee, Cha rge_final.income_id, charge_final.payment_id, charge_final.note from {$this->charge_final} as Charge_fina L LEFT Join {$this-> Bass_productline} as Bass_productline on charge_final.channel_id=bass_productline.channel_id and Charge_ Final.channel_type=bass_productline.channel_type where 1=1 $conditions order by Charge_final.charge_time de   SC "; $query = $this->db->query ($sql);//Counter $cnt = 0;//every $limit line, refresh the output buffer, not too big or too small $limit = 8000;foreach ($query ->result_array () as $row) {$cnt ++;if ($limit = = $cnt) {//Refresh output buffer to prevent problems caused by excessive data ob_flush (); flush (); $cnt = 0;} $income _id= $row [' income_id ']==0? ' No money back ': ' return money '; $payment _id= $row [' payment_id ']==0? ' Not mentioned ': ' has been raised ';//read table Data $content = Array (), $content [] = Iconv (' utf-8 ', ' GBK ', $row [' charge_id '].  "\ t"); $content [] = "; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' product_name ']); $content [] ="; $content [] = Iconv (' Utf-8 ', ' GBK ', $row [' business_id ']. "\ t"); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' channel_id ']. "\ t"); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Channel_type ']$content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' product_line_name ']); $content [] = "; $content [] = Iconv (' Utf-8 ', ' GBK ', $row [' Business_linkid ']); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' fee ']); $conte  NT [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Msgfrom ']); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Charge_time ']. "\ t"); $content [] = "; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' income_contract ']); $content [] ="; $content [] = IC Onv (' Utf-8 ', ' GBK ', $row [' income_channel_name ']); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Payment  _contract ']); $content [] = "; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' merchant_name ']); $content [] ="; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Platform ']); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Product_type ') ]); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' bill_time ']. ' \ t '); $content [] = "; $content [] = Iconv (' Utf-8 ', ' GBK ', $row [' channel_rate ']), $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Channel_fee ']); $con tent [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' account ']); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' payment_rate ']), $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' Payment_fee ']); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $income _id); $content [] = '; $content [] = Iconv (' utf-8 ', ' GBK ', $payment _id); $co ntent [] = "; $content [] = Iconv (' utf-8 ', ' GBK ', $row [' note ']); Fputcsv ($fp, $content);}
Third, the tail of the document

                      $sql = "Select sum (charge_final.fee) as Amountorder,sum (Charge_final.channel_fee) as Sumchannel_fee,su M (Charge_final.fee-charge_final.channel_fee) as Sumaccout,sum (Charge_final.payment_fee) as Sumpayfeefrom {$this- >charge_final} as Charge_final left join {$this->bass_productline} as Bass_productline on charge_final.channel_id =bass_productline.channel_id and Charge_final.channel_type=bass_productline.channel_typewhere 1=1 $conditions "; $ Querys = $this->db->query ($sql), $ro = $querys->result_array (), $this->data [' ro '] = $ro; $amountorder = $ro [' 0 ' [' amountorder ']; $sumchannel _fee= $ro [' 0 '] [' sumchannel_fee ']; $sumaccout = $ro [' 0 '] [' sumaccout ']; $sumpayfee = $ro [ ' 0 ' [' sumpayfee ']; $foot = Array (' Stats ', ' ', ', ' ', ', ', ', ', ', ', ', ', ', ', ', ', ' $amountorder ', ', ', ', ', ', ', ', ', ', ', ', ', ', ',  ', ', ', ', ', ', ', ', ', ', ' $sumchannel _fee, ' ', ' $sumaccout ', ' ', ' ', ' ', ' $sumpayfee ', ' ', ', ', ', ', '; foreach ($foot as $i + $v) {//CSV Excel supports GBK encoding, be sure to convert otherwise garbled $foot[$i] = iconv (' utf-8 ', ' GBK ', $v); }//Writes data through Fputcsv to the file handle Fputcsv ($FP, $foot);}}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.