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