导出数据到表格PHP

php

浏览数:325

2019-1-7

public function excel(){
    $filename = '导出表格';
    $header = ['编号','名称'];
    $index = ['id','name'];
    $list = array(
        0 => array('id' => 1, 'name' => 'fdfs'),
        1 => array('id' => 2, 'name' => 'ffdfdsafs'),
        2 => array('id' => 3, 'name' => 'fdfgdgsddfafass'),
    );
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:filename=".$filename.".xls");
    $table_header = rtrim(implode("\t",$header), "\t");
    $strexport = $table_header."\r";
    foreach ($list as $row){
        foreach($index as $val){
            $strexport .= $row[$val] . "\t";
        }
        rtrim($strexport,"\t");
        $strexport .= "\r";
    }
    $strexport=iconv('UTF-8',"GB2312//IGNORE", $strexport);
    exit($strexport);
}