Thinkphp exports table data to the excel file
Reply to discussion (solution)
/*** Export data into an excel table * @ param $ data: A two-dimensional array. The structure is like the array found from the database * @ param $ title: the first row title of excel, an array, if it is null, no title * @ param $ filename indicates the downloaded file name * @ examlpe $ stu = M ('user'); $ arr = $ stu-> select (); exportexcel ($ arr, array ('id', 'account', 'password', 'Nickname '), 'File name! '); */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 to 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 into an excel table * @ param $ data: A two-dimensional array. The structure is like the array found from the database * @ param $ title: the first row title of excel, an array, if it is null, no title * @ param $ filename indicates the downloaded file name * @ examlpe $ stu = M ('user'); $ arr = $ stu-> select (); exportexcel ($ arr, array ('id', 'account', 'password', 'Nickname '), 'File name! '); */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 to 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 $ is filename the value passed from the front-end page?
The value you pass from the front-end should be processed by the backend before you call this method to pass parameters.
Learning, good