## HTML
### 按钮
```
<a href="javascript:exportReport()">
<div class="add" title="选定行数据导出excel文件,如果不选中行,将导出列表所有数据">
<span><i class="fa fa-plus"></i>导出数据</span>
</div>
</a>
```
### form
```
<form class="navbar-form form-inline" method="post" action="{:U('Admin/order/export_order')}" name="search-form2"
id="search-form2">
</form>
```
### div table
```
<div id="flexigrid" cellpadding="0" cellspacing="0" border="0">
<table>
<tbody>
<tr data-order-id="1541" id="1541">
<td class="sign" axis="col0">
<div style="width: 24px;"><i class="ico-check"></i></div>
</td>
<td align="left" abbr="order_sn" axis="col3" class="">
<div style="text-align: left; width: 140px;" class="">201711081606393250</div>
</td>
<td align="left" abbr="consignee" axis="col4" class="">
<div style="text-align: left; width: 120px;" class="">杨红伟:18903467858</div>
</td>
<td align="center" abbr="article_show" axis="col5" class="">
<div style="text-align: center; width: 60px;" class="">0.01</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 60px;" class="">0.01</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 60px;" class="">待确认</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 60px;" class="">未支付</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 60px;" class="">未发货</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 60px;" class="">PC端支付宝</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 60px;" class="">申通物流</div>
</td>
<td align="center" abbr="article_time" axis="col6" class="">
<div style="text-align: center; width: 120px;" class="">2017-11-08 16:06</div>
</td>
<td align="left" axis="col1" class="handle">
<div style="text-align: left; ">
<a class="btn green" href="/index.php/Admin/order/detail/order_id/1541"><i
class="fa fa-list-alt"></i>查看</a>
<!---->
</div>
</td>
<td style="width: 100%;">
<div></div>
</td>
</tr>
</tbody>
</table>
</div>
```
## JS
```
<script>
function exportReport() {
var selected_ids = '';
$('.trSelected', '#flexigrid').each(function (i) {
selected_ids += $(this).data('order-id') + ',';
});
if (selected_ids != '') {
$('input[name="order_ids"]').val(selected_ids.substring(0, selected_ids.length - 1));
}
$('#search-form2').submit();
}
</script>
```
## PHP
```
public function export_order()
{
//搜索条件
$consignee = I('consignee');
$order_sn = I('order_sn');
$timegap = I('timegap');
$order_status = I('order_status');
$order_ids = I('order_ids');
$where = array();//搜索条件
if($consignee){
$where['consignee'] = ['like','%'.$consignee.'%'];
}
if($order_sn){
$where['order_sn'] = $order_sn;
}
if($order_status){
$where['order_status'] = $order_status;
}
if($timegap){
$gap = explode('-', $timegap);
$begin = strtotime($gap[0]);
$end = strtotime($gap[1]);
$where['add_time'] = ['between',[$begin, $end]];
}
if($order_ids){
$where['order_id'] = ['in', $order_ids];
}
$orderList = Db::name('order')->field("*,FROM_UNIXTIME(add_time,'%Y-%m-%d') as create_time")->where($where)->order('order_id')->select();
$strTable ='<table width="500" border="1">';
$strTable .= '<tr>';
$strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单编号</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="100">日期</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">收货人</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">收货地址</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">电话</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单金额</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">实际支付</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付方式</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付状态</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">发货状态</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">商品数量</td>';
$strTable .= '<td style="text-align:center;font-size:12px;" width="*">商品信息</td>';
$strTable .= '</tr>';
if(is_array($orderList)){
$region = get_region_list();
foreach($orderList as $k=>$val){
$strTable .= '<tr>';
$strTable .= '<td style="text-align:center;font-size:12px;"> '.$val['order_sn'].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['create_time'].' </td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['consignee'].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'."{$region[$val['province']]},{$region[$val['city']]},{$region[$val['district']]},{$region[$val['twon']]}{$val['address']}".' </td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['mobile'].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_price'].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['order_amount'].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['pay_name'].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$this->pay_status[$val['pay_status']].'</td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$this->shipping_status[$val['shipping_status']].'</td>';
$orderGoods = D('order_goods')->where('order_id='.$val['order_id'])->select();
$strGoods="";
$goods_num = 0;
foreach($orderGoods as $goods){
$goods_num = $goods_num + $goods['goods_num'];
$strGoods .= "商品编号:".$goods['goods_sn']." 商品名称:".$goods['goods_name'];
if ($goods['spec_key_name'] != '') $strGoods .= " 规格:".$goods['spec_key_name'];
$strGoods .= "<br />";
}
unset($orderGoods);
$strTable .= '<td style="text-align:left;font-size:12px;">'.$goods_num.' </td>';
$strTable .= '<td style="text-align:left;font-size:12px;">'.$strGoods.' </td>';
$strTable .= '</tr>';
}
}
$strTable .='</table>';
unset($orderList);
downloadExcel($strTable,'order');
exit();
}
```
## 函数
```
/**
* 导出excel
* @param $strTable 表格内容
* @param $filename 文件名
*/
function downloadExcel($strTable,$filename)
{
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d').".xls");
header('Expires:0');
header('Pragma:public');
echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
}
```