-
-
/** - * Output CSV header information
- * Note: There should be no data output before and after using this function
- * @param $data Array Downloaded data
- * @param $file_name String Downloaded file name
- * @edit: bbs. it-home.org
- */
- function outputCsvHeader($data,$file_name = 'export')
- {
- header('Content-Type: text/csv ');
- $str = mb_convert_encoding($file_name, 'gbk', 'utf-8');
- header('Content-Disposition: attachment;filename="' .$str . '.csv"');
- header ('Cache-Control:must-revalidate,post-check=0,pre-check=0');
- header('Expires:0');
- header('Pragma:public');
- $csv_data = '' ;
- foreach ($data as $line)
- {
- foreach ($line as $key => &$item)
- {
- $item = str_replace (',',',',str_replace(PHP_EOL,'', $item)); //Filter (,) commas and line breaks in the generated csv file
- $item = mb_convert_encoding($item, 'gbk', 'utf-8');
- }
- $csv_data .= implode(', ', $line) . PHP_EOL;
- }
- echo $csv_data;
- }
//Example of php exporting csv file
- outputCsvHeader($data,"myfile.csv"); p>
-
Copy code
|