thinkphp Exporting report data to an Excel file

Source: Internet
Author: User
thinkphp exporting tabular data to an Excel file
thinkphp exporting tabular data to an Excel file
------Solution--------------------

/**
* Export data as Excel table
[Email protected] $data a two-dimensional array, structured like an array found from a database
[Email protected] $title The first row of Excel header, an array, if empty, no caption
[Email protected] $filename The file name of the download
[Email protected]
$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);
}
}

------Solution--------------------
Study, good
  • 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.