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