从数据库导出Excel文或者cvs文件

<?php
/*header('Content-type: text/html; charset=utf-8');
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=test.xls");*/

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="file.cvs"');//文件name
header('Cache-Control: max-age=0');


$conn = mysqli_connect("192.168.22","root","mima") or die("无法连接数据库");
mysqli_select_db($conn,"ave40_mg");
mysqli_set_charset($conn,'utf8');

echo "id\tperiod\tstore_id\t\n";

/*     $addo="o.`increment_id` ,o.`state`, o.`status`, o.`grand_total`, o.`created_at`";
     $adda="a.`region`, a.`city`,  a.`firstname`, a.`middlename`, a.`lastname`";
     $addp="p.`method`";
     */
     
     
     
$fieldArry=array(" o.`state`", "o.`status`", "o.`grand_total`", "o.`created_at`","p.`method`","a.`region`",
                  "a.`city`", "a.`street`","a.`firstname`", "a.`middlename`", "a.`lastname`");//"a.`street`",
$field="o.`increment_id`";
$line="10";

foreach($fieldArry as $key => $value ){
      $field=$field .", ".$value;
}

$sql = "SELECT   $field

    FROM  `sales_flat_order` o
    
    INNER JOIN `sales_flat_order_payment` p
        ON p.`parent_id`=o.`entity_id`
        
    INNER JOIN `sales_flat_order_address` a
        ON a.`parent_id`=o.`entity_id`
        AND a.address_type = 'shipping'
        
    WHERE o.`state`='processing'
       ORDER BY o.`increment_id` DESC
    LIMIT $line";

 $result = mysqli_query($conn, $sql);




//方法1
foreach ($result as $row => $value) {
    echo "$value[increment_id] \t";
    echo "$value[state] \t";
    echo "$value[status] \t";
    echo "$value[grand_total] \t";
    echo "$value[created_at] \t";
    echo "$value[method] \t";
    echo "$value[region] \t";
    echo "$value[city] \t";
    /*     echo "$value[street] \t";*/
    echo str_replace("\n", "", "$value[street] \t");
    //去除字段里面的换行
    echo "$value[firstname] \t";
    echo "$value[middlename] \t";
    echo "$value[lastname] \t";
    echo "\t\n";
    
}
//方法2

/*
 while ($row = mysqli_fetch_array($result)) {
     
     for ($i = 0; $i<count($fieldArry)+1; $i++) {
      
      echo str_replace("\n", "", "$row[$i] \t");
     }
     echo "\t\n";
  
    
 }*/
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

onlyxiaobo1234

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值