functionResult_export ($title,$list,$field) {//Call Php_excelVendor ("Phpexcel.phpexcel"); $objExcel=NewPhpexcel (); $objWriter=NewPhpexcel_writer_excel2007 ($objExcel); $objExcel->setactivesheetindex (0); $objActSheet=$objExcel-Getactivesheet (); $objActSheet->settitle ($title); $letter= ' A '; foreach($field as $values) { $objActSheet->setcellvalue ($letter. ' 1 ',$values); ++$letter; } if(Is_array($list)) { foreach($list as $k=$v) { $letter= ' A '; $k=$k+ 2; Reset($field); foreach($field as $key=$value) { $objActSheet->setcellvalueexplicit ($letter.$k,$v[$key], phpexcel_cell_datatype::type_string);//Setcellvalue ($letter. $k, $v [$key])++$letter; } } } $objExcel->setactivesheetindex (0); //If AutoLoad is used in the frame, it will conflict with Phpexcel's autoload and need to be re-registered, such as: Spl_autoload_register ( Array (' Think ', ' autoload ')); Header("Content-type:application/force-download"); Header("Content-type:application/octet-stream"); Header("Content-type:application/download"); Header(' Content-disposition:inline;filename= '. mb_convert_encoding ($title, ' gb2312 ', ' UTF-8 '). '. xlsx '); Header("Content-transfer-encoding:binary"); Header("Last-modified:".)gmdate("D, D M Y h:i:s"). "GMT"); Header("Cache-control:must-revalidate,post-check=0,pre-check=0"); Header("Pragma:no-cache"); $objWriter->save (' Php://output ');}functionResult_export_csv ($title,$list,$field) { $filename=$title. ". csv"; 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 '); $data=implode(‘,‘,$field) .Php_eol; if(Is_array($list)) { foreach($list as $k=$v) { Reset($field); $row= ' '; foreach($field as $key=$value) { $row.=$v[$key] . ‘,‘; } $data.=RTrim($row, ‘,‘) .Php_eol; } } Echo $data;}/** * Export data from Database * @param type $head $head = Array (' name ' = ' ' name ', ' gender ' = ' gender ', ' age ' = ' ages ', ' E Mail ' = ' Email ', ' phone ' = ' telephone ', ' ... ' * @param type $list*/functionExportcsv ($head,$list,$title= ' ') { //$this->outputcsv ($head, $list, date (' Ymdhis ')); Output Excel file header, you can replace user.csv with the file name you want Header(' Content-type:application/vnd.ms-excel '); Header(' Content-disposition:attachment;filename = "'. ($title?$title:Date(' Ymdhis ')) . '. csv '); Header(' Cache-control:max-age=0 '); $fp=fopen(' Php://output ', ' a '); //output Excel column name information 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); foreach($list as $val) { foreach($head as $i=$v) { $row[] = mb_convert_encoding ($val[$i], ' GBK ', ' utf-8 '); } fputcsv ($fp,$row); } fclose($fp);}
PHP Export Excel,csv