thinkphp exporting tabular data to an Excel file
Reply to discussion (solution)
/** * Export data to Excel table * @param $data a two-dimensional array, structured like an array from a database * @param $title the first row of Excel header, an array, if empty, no title * @param $filename the downloaded file name * @examlpe $stu = M (' User '); $arr = select ($stu); Exportexcel ($arr, array (' ID ', ' account ', ' Password ', ' nickname '), ' filename! '); */function Exportexcel ($data =array (), $title =array (), $filename = ' report ') {header ("content-type:application/ Octet-stream "); Header ("Accept-ranges:bytes"); Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:attachment;filename=". $filename. ". XLS "); Header ("Pragma:no-cache"); Header ("expires:0"); Export xls start if (!empty ($title)) {foreach ($title as $k = = $v) {$title [$k]=iconv ("UTF-8", "GB2312", $V); } $title = implode ("\ T", $title); echo "$title \ n"; } if (!empty ($data)) {foreach ($data as $key = = $val) {foreach ($val as $ck + = $CV) { $data [$key] [$ck]=iconv ("UTF-8", "GB2312", $CV); } $data [$key]=implode ("\ t", $data [$key]); } echo implode ("\ n", $data); } }
/** * Export data to Excel table * @param $data a two-dimensional array, structured like an array from a database * @param $title the first row of Excel header, an array, if empty, no title * @param $filename the downloaded file name * @examlpe $stu = M (' User '); $arr = select ($stu); Exportexcel ($arr, array (' ID ', ' account ', ' Password ', ' nickname '), ' filename! '); */function Exportexcel ($data =array (), $title =array (), $filename = ' report ') {header ("content-type:application/ Octet-stream "); Header ("Accept-ranges:bytes"); Header ("Content-type:application/vnd.ms-excel"); Header ("Content-disposition:attachment;filename=". $filename. ". XLS "); Header ("Pragma:no-cache"); Header ("expires:0"); Export xls start if (!empty ($title)) {foreach ($title as $k = = $v) {$title [$k]=iconv ("UTF-8", "GB2312", $V); } $title = implode ("\ T", $title); echo "$title \ n"; } if (!empty ($data)) {foreach ($data as $key = = $val) {foreach ($val as $ck + = $CV) { $data [$key] [$ck]=iconv ("UTF-8", "GB2312", $CV); } $data [$key]=implode ("\ t", $data [$key]); } echo implode ("\ n", $data); } }
$date $title $filename is the value of the foreground page coming up?
The value of your front desk is best handled by the background and then call this method to pass parameters
Study, good