PHP Excel 导出实例

334 查看

通过全面form提交过来的参数,实现数据库表中数据导出,比PHPExcel类库高效N倍;
注意“表头”第一个字段不要用英文,excel表头字段不要有换行。

header("Content-type:text/html;charset=uft-8");

require_once 'connect.php';

export_csv();

function export_csv() {
    $filename = 'fh_'.date('YmdHis').".csv";//文件名
    header("Content-type:text/csv");
    header("Content-Disposition:attachment;filename=".$filename);
    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
    header('Expires:0');
    header('Pragma:public');
    echo array_to_string(get_export_data());
}

function i($strInput) {
    return iconv('utf-8','gb2312',$strInput);//页面编码为utf-8时使用,否则导出的中文为乱码
}

function array_to_string($result) {
    if(empty($result)) {
        return i("没有符合您要求的数据!");
    }

    //表头
    $data = iconv(
        'utf-8',
        'gb2312',
        "平台,店铺,仓库,订单号,交易号,发货日期,下单时间,支付时间,快递公司,物流单号,昵称,收件人,电话,款号,颜色名称,尺码名称,SKU,产品名称,数量,均摊价")."\r\n";

    // 总记录条数
    $size_result = sizeof($result);

    //,商家留言 .i($result[$i]['say'])
    for($i = 0 ; $i < $size_result ; $i++) {
        $data .= 
                i($result[$i]['pt']).','.
            i($result[$i]['dp']).','.
            i($result[$i]['ck']).','.
            i($result[$i]['order_sn']).','.
            i($result[$i]['deal_code']).','.
                i($result[$i]['fhrq']).','.
                i($result[$i]['xdrq']).','.
                i($result[$i]['zfrq']).','.
                i($result[$i]['kd']).','.
                i($result[$i]['wldh']).','.
                i($result[$i]['nick']).','.
                i($result[$i]['sjr']).','.
                i($result[$i]['phone']).','.
                i($result[$i]['goods_sn']).','.
                i($result[$i]['color_name']).','.
                i($result[$i]['size_name']).','.
                i($result[$i]['sku']).','.
                i($result[$i]['goods_name']).','.
                i($result[$i]['num']).','.
                i($result[$i]['pay'])."\n";
    }
    return $data;
}

function get_export_data() {

    $sql = "select 
                pt,dp,ck,order_sn,deal_code,fhrq,xdrq,
                zfrq,kd,wldh,nick,sjr,phone,goods_sn,
                color_name,size_name,sku,goods_name,
                num,pay,say
             from 
                oiwas
             limit 10000";              

    $result = mysql_query($sql);

    $res = array();
    $i = 0;
    while(!!$row = mysql_fetch_array($result)) {
        $res[$i]['pt'] = $row['pt'];
        $res[$i]['dp'] = $row['dp'];
        $res[$i]['ck'] = $row['ck'];
        $res[$i]['order_sn'] = "'".$row['order_sn'];
        //$res[$i]['deal_code'] = $row['deal_code'];
        $res[$i]['deal_code'] = "'".str_replace(',', '-', $row['deal_code']);
        $res[$i]['fhrq'] = $row['fhrq'];
        $res[$i]['xdrq'] = $row['xdrq'];
        $res[$i]['zfrq'] = $row['zfrq'];
        $res[$i]['kd'] = $row['kd'];
        $res[$i]['wldh'] = "'".$row['wldh'];
        $res[$i]['nick'] = $row['nick'];
        $res[$i]['sjr'] = $row['sjr'];
        $res[$i]['phone'] = "'".$row['phone'];
        $res[$i]['goods_sn'] = $row['goods_sn'];
        $res[$i]['color_name'] = $row['color_name'];
        $res[$i]['size_name'] = $row['size_name'];
        $res[$i]['sku'] = $row['sku'];
        $res[$i]['goods_name'] = $row['goods_name'];
        $res[$i]['num'] = $row['num'];
        $res[$i]['pay'] = $row['pay'];
        $i++;
    }
    return $res;
}