实现简单的php导出excel

            $filename=date("Y年m月d日")."数据信息统计结果.xls";     //文件名
            $ua = $_SERVER["HTTP_USER_AGENT"];        //中文文件名不乱码
            if (preg_match("/MSIE/", $ua)) {
                  $filename=urlencode($filename);
            } else if (preg_match("/Firefox/", $ua)) {
                  $filename=$filename;
            } else {
                  $filename=urlencode($filename);
            }

            header("Content-type:application/vnd.ms-excel");   //输出excel头
            header("Content-Disposition:filename=".$filename);
            $num_header = iconv(‘utf-8‘, ‘gbk‘, ‘11‘);       //设置excel表格列
            $title_header = iconv(‘utf-8‘, ‘gbk‘, ‘22‘);
            $keyword_header = iconv(‘utf-8‘, ‘gbk‘, ‘33‘);
            $sendunit_header = iconv(‘utf-8‘, ‘gbk‘, ‘44‘);
            $receiveunit_header = iconv(‘utf-8‘, ‘gbk‘, ‘55‘);
            $addname_header = iconv(‘utf-8‘, ‘gbk‘, ‘66‘);
            
            echo "{$num_header}\t";
            echo "{$title_header}\t";
            echo "{$keyword_header}\t";
            echo "{$sendunit_header}\t";
            echo "{$receiveunit_header}\t";
            echo "{$addname_header}\t\n";

                  
            foreach($a as $kk=>$pp){          //对每列些数据
                  foreach($pp[‘child‘] as $k=>$p){
                        echo iconv("utf-8", "gbk//IGNORE", $pp[‘u‘]."\t");
                        echo iconv("utf-8", "gbk//IGNORE", $p[‘u‘]."\t");
                        echo iconv("utf-8", "gbk//IGNORE", $p[‘s‘]."\t");
                        echo iconv("utf-8", "gbk//IGNORE", $p[‘t‘]."\t");
                        echo iconv("utf-8", "gbk//IGNORE", $p[‘c‘]."\t");
                        echo iconv("utf-8", "gbk//IGNORE", $p[‘t‘]."\t\n");
                  }
            }

 

数组格式为

Array
(
    [0] => Array
        (
            [unitid] => 1234
            [u] => aaa
            [fid] => 1000
            [shengid] => 1000
            [shiid] => 0
            [xianid] => 0
            [rid] => 2
            [orders] => 50
            [sys_createuid] => 1
            [sys_createtime] => 1383556976
            [sys_updateuid] => 1
            [sys_updatetime] => 1389853763
            [child] => Array
                (
                    [0] => Array
                        (
                            [unitid] => 1035
                            [u] => bbb
                            [fid] => 1000
                            [shengid] => 1000
                            [shiid] => 0
                            [xianid] => 0
                            [rid] => 2
                            [orders] => 50
                            [sys_createuid] => 1
                            [sys_createtime] => 1383556976
                            [sys_updateuid] => 1
                            [sys_updatetime] => 1389853763
                            [funitname] => 
                            [s] => 6
                            [t] => 1
                            [c] => 1
                            [t] => 2
                        )
           [4] => Array
                        (
                            [u] => 合计
                            [s] => 8
                            [t] => 1
                            [c] => 3
                            [t] => 3
                        )

            )

        )
)

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。