首页 » PHP » 从数据库导出Excel文或者cvs文件

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

原文 http://blog.csdn.net/onlyxiaobo1234/article/details/79215732

2018-02-01 02:00:19阅读(485)

<?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";
  
    
 }*/

最新发布

CentOS专题

关于本站

5ibc.net旗下博客站精品博文小部分原创、大部分从互联网收集整理。尊重作者版权、传播精品博文,让更多编程爱好者知晓!

小提示

按 Ctrl+D 键,
把本文加入收藏夹