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;
}

 

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