php輸出xls,長數字原樣輸出方案

 

public function downCardList($product_id){
        $product = model('Product')->getOneData(' * ',['id'=>$product_id]);
        $card = model('CardType')->getOneData(' * ',['id'=>$product['cardtype']]);
        $table = 'card_'.$card->mark;

        $data = Db::table($table)->field('*')->where(['product_id'=>$product_id])->select();
        //到這裏都是獲取信息
        //開始設置內容
        $str  = ' < html>';
        $str .= ' < head>';
        $str .= ' < meta http - equiv = "Content-Type" content = "text/html; charset=utf-8" > ';
        $str .= '</head > ';
        $str .= '<body > ';
        $str .= '<table width = "16rem" border = "1" align = "center" cellspacing = "1" cellpadding = "1" > ';
        $str .= '<tr align = "center" > ';

        $str .= "<td nowrap ><b > 產品名稱</b ></td > <td nowrap ><b > ".$product->name."</b ></td > ";
        $str .= '</tr > ';
        $str .= '<tr align = "center" > ';
        $str .= "<td nowrap ><b > 產品數量</b ></td > <td nowrap ><b > ".$product->num."</b ></td > ";
        $str .= '</tr > ';
        $str .= '<tr align = "center" > ';
        $str .= "<td nowrap ><b > 產品建立時間</b ></td > <td nowrap ><b > ".$product->create_time."</b ></td > ";

        $str .= '</tr > ';
        $str .= '<tr align = "center" > ';
        $str .= '<td nowrap ><b > 卡號</b ></td > <td nowrap ><b > 激活碼</b ></td >';
        $str .= '</tr > ';
        //遍歷主體內容
        foreach ($data as $val){
            $str .= '<tr align = "center" > ';
            //數字每四位加空格,方便看
            $psw = substr_replace($val['password'],' ',4,0);
            $psw = substr_replace($psw,' ',9,0);
            $psw = substr_replace($psw,' ',14,0);
            $str .= "<td nowrap >".$card->mark.$val['id']."</td > <td nowrap style='vnd.ms-excel.numberformat:@'>".$psw."</td > ";//vnd.ms-excel.numberformat:@將表格設置爲文本,以便長數字所有正常顯示
            $str .= '</tr > ';
        }
        $str .= '</table > ';
        $str .= '</body > ';
        $str .= '</html > ';

        header("cache-control:no-cache,must-revalidate");//無瀏覽器緩存
        header("Content-Type:application/vnd.ms-execl");//提示下載爲Excel
        header("Content-Type:application/octet-stream");
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=".$product->name.'-'.date('Y-m-d').".xls");
        header('Expires:0');
        header('Pragma:public');
        echo "\xFF\xFE".mb_convert_encoding( $str, 'UCS-2LE', 'UTF-8' );
    }
相關文章
相關標籤/搜索