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

2018-02-03 10:40:58来源:网络收集作者:纳米程序员人点击

分享
<?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; $iecho str_replace("/n", "", "$row[$i] /t");
}
echo "/t/n"; }*/

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台