php导出execl

<?php
function export_excel($items,$fields,$fields_array,$name)
{  
      /*
      * 调用方法示例
      * $items = $this->mysql->getarr("select * from {$this->pre}user $where order by uid desc");
      * $fields=array('username','money','regtime');
      * $fields_array = array('username'=>'用户名','money'=>'电子币','regtime'=>'注册时间');
      * export_excel($items,$fields,$fields_array,date('Y-m-d').'会员资料');
      */
      header("Content-type:application/vnd.ms-excel");
      header("Content-Disposition:filename=".$name.".xls");
      $str = "<table border=1><tr>";
      foreach ($fields as $value) {
        $str .= "<th>".iconv("UTF-8","GB2312",$fields_array[$value])."</th>";
      }
      $str .= "</tr>";
      foreach ($items as $item) {
        $str .= "<tr>";
        foreach ($fields as $value) { 
            // $val=
            if(strstr($value,'time')){
                if(is_int($item[$value]+0)){ 
                    $item[$value]=date('Y-m-d H:i:s',$item[$value]);
                } 
            }
            $str .= "<td>".iconv("UTF-8","GB2312",$item[$value])."</td>";
        }
        $str .= "</tr>";
      }
      $str .= "</table>";
      $str .="<script>window.close();</script>";
      echo $str;
}
原文地址:https://www.cnblogs.com/mssql8/p/4477516.html