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