Examples of how PHP generates. csv suffix file tables

Source: Internet
Author: User

This time to give you an example of PHP generated. csv suffix file table, mainly in code form, the following is the actual case, together with the small series to see.

Learn how to build a method with the suffix. csv file first

   # array, call the following method and then directly download public    function Index () {        $array = [            ' name ' = ' Zhang San ', ' age ' = ' = ', ' '            name ' = ' = ' John Doe ', ' age ' = ' +            ', [' name ' = ' Harry ', ' age ' +],            [' name ' = ' Hemp II ', ' age ' = ', ']        ;        return $this->export_csv ($array);    }
/***     * Export download Excel     * @param [array] $array [to convert an Excel array] *    /Public Function export_csv ($array) {        $ temp = ';        foreach ($array as $value)        {            foreach ($value as $k = $val)            {                $value [$k] = iconv (' utf-8 ', ' gb2312 ', $ value[$k]);            }            $temp. = Implode (",", $value). " \ n "; Separate with "comma" in English, get the value        }        # get Name,age to do the title "Here I didn't think of a better way to make a flag in order to be more perfect later"        # # is to take out ' name ', ' age ' two key        $ KeyName = Array_keys ($array [0]) [0]. ',' . Array_keys ($array [0]) [1]. " \ n ";        $string  = $keyname. $temp;//stitching        $filename = date (' Ymd '). CSV '; Set the file name        header ("Content-type:text/csv");        Header ("Content-disposition:attachment;filename=". $filename);        Header (' cache-control:must-revalidate,post-check=0,pre-check=0 ');        Header (' expires:0 ');        Header (' Pragma:public ');        echo $string;    }

The final exported Excel is as follows:

The above is a simple Excel method that does not use class library generation!

Related recommendations:

PHP file Export-excel and CSV

Generate a CSV file with PHP

Resolve PHP Output CSV file Chinese garbled

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.