用php把資料匯出excel

dead_lee發表於2021-09-09

一:先獲取資料

<!--more--&gt

public function output($dataResult=array()){
    $headTitle = date('YmdHis');
    $title = date('YmdHis');
    $headtitle= "{$headTitle}";
    $titlename = '';
    $headtitle = "
ID
           條碼號
           商品組合
           專案明細
           病人
           醫生姓名
           醫生折扣
           代理姓名
           業務員
           原價
           實付
           退款
           日期
       ";
    $filename = $title.".xls";
    $this->excelData($dataResult,$titlename,$headtitle,$filename);
}

二:輸出

    /*
 *處理Excel匯出
 *@param $datas array 設定表格資料
 *@param $titlename string 設定head
 *@param $title string 設定表頭
 */
private function excelData($datas,$titlename,$title,$filename){
    $str = "rnrnrnrn";
    $str .="
".$titlename."";
    $str .= $title;
    foreach ($datas  as $key=> $rt )
    {
        $str .= "";
        foreach ( $rt as $k => $v )
        {
            $str .= "";
        }
        $str .= "n";
    }
    $str .= "
'{$v}
"; header("Content-type:application/vnd.ms-excel"); //header( "Content-Type: application/vnd.ms-excel; name='excel'" ); //header( "Content-type: application/octet-stream" ); header( "Content-Disposition: attachment; filename=".$filename ); //header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" ); header( "Pragma: no-cache" ); header( "Expires: 0" ); exit( $str ); }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/4686/viewspace-2799408/,如需轉載,請註明出處,否則將追究法律責任。

相關文章