thinkphp exporting tabular data to an Excel file

Source: Internet
Author: User
Tags ranges
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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.