全部导出和时间导出
html代码,全程并不需要引用什么插件
<include file="public@header"/> <link href="__TMPL__/public/assets/css/style.css" rel="stylesheet"> </head> <body> <div class="margin-top-20 body_top"> <div class="form-inline"> <input type="submit" name="all" class="btn btn-info quanbu" value="全部导出"/> <input style="margin-left: 15px" type="text" name="data" placeholder="日期范围" class="form-control month" id="test6" autocomplete="off"> <input type="submit" class="btn btn-info shijian" value="导出"/> </div> </div> </body> <script src="__STATIC__/js/admin.js"></script> <script src="__STATIC__/js/laydate/laydate.js"></script> <script> //日期范围 laydate.render({ elem: '#test6' ,range: true }); $('.quanbu').on('click',function () { $.ajax({ url: "/systems/Sum/quanbu", type: "post", dataType: "json", // data:{'data':nian}, success: function (back) { if (back.code == 1) { document.location.href = (back.data); } else { layer.msg("导出csv出错!"); } }, error: function (e) { console.log(e) } }); }); $('.shijian').on('click',function () { var data=$('.month').val(); $.ajax({ url: "/systems/Sum/shijian", type: "post", dataType: "json", data:{'data':data}, success: function (back) { if (back.code == 1) { document.location.href = (back.data); } else { layer.msg("导出csv出错!"); } }, error: function (e) { console.log(e) } }); }) </script>
php代码
public function quanbu(){ //全部导出 $req=Db::name('cy_sum') ->alias('a') ->field('s.store_name,b.company_name as bloc_name,d.company_name as dealer_name,a.type,a.garment,a.garment_function,a.clothing,a.clothing_function,a.functional_finishing,a.create_time') ->join('store s','a.sid=s.id') ->join('bloc b','a.bid=b.id') ->join('dealer d','a.did=d.id') ->select() ->toArray(); foreach ($req as $k=>$v){ switch ($req[$k]['type']) { case 1: $req[$k]['type'] = '定制'; break; case 2: $req[$k]['type'] = '定制团单'; break; case 3: $req[$k]['type'] = '样衣'; break; case 4: $req[$k]['type'] = '大货'; break; case 5: $req[$k]['type'] = '面料试样'; break; case 6: $req[$k]['type'] = '售后订单'; break; } } $csv_data = ''; //标题 $csv_data .= "门店,集团,经销商,订单类型,成衣数量,成衣+功能数量,制衣数量,制衣+功能数量,后整理加工,日期 "; $row = array(); foreach ($req as $k=>$val){ $row['store_name'] = $val['store_name']; $row['bloc_name'] = $val['bloc_name']; $row['dealer_name'] = $val['dealer_name']; $row['type'] = $val['type']; $row['garment'] = $val['garment']; $row['garment_function'] = $val['garment_function']; $row['clothing'] = $val['clothing']; $row['clothing_function'] = $val['clothing_function']; $row['functional_finishing'] = $val['functional_finishing']; $row['create_time'] = date('Y-m-d',$val['create_time']); $csv_data .= join(',', array_values($row))." "; } $file_name = date('YmdHis', time()).'detail.csv'; //导出数据 $url = save_csv($file_name, $csv_data); $this->success('成功!','',$url); exit(); } public function shijian(){ $list=$this->request->param(); //时间导出 $data=explode("- ",$list['data']); $data[0]=strtotime($data[0]); $data[1]=strtotime($data[1]); $req=Db::name('cy_sum') ->alias('a') ->field('s.store_name,b.company_name as bloc_name,d.company_name as dealer_name,a.type,a.garment,a.garment_function,a.clothing,a.clothing_function,a.functional_finishing,a.create_time') ->join('store s','a.sid=s.id') ->join('bloc b','a.bid=b.id') ->join('dealer d','a.did=d.id') ->whereTime('a.create_time', 'between', [$data[0], $data[1]]) ->select() ->toArray(); if (empty($req)){ $this->error('该时间段无数据 !'); } foreach ($req as $k=>$v){ switch ($req[$k]['type']) { case 1: $req[$k]['type'] = '定制'; break; case 2: $req[$k]['type'] = '定制团单'; break; case 3: $req[$k]['type'] = '样衣'; break; case 4: $req[$k]['type'] = '大货'; break; case 5: $req[$k]['type'] = '面料试样'; break; case 6: $req[$k]['type'] = '售后订单'; break; } } $csv_data = ''; //标题 $csv_data .= "门店,集团,经销商,订单类型,成衣数量,成衣+功能数量,制衣数量,制衣+功能数量,后整理加工,日期 "; $row = array(); foreach ($req as $k=>$val){ $row['store_name'] = $val['store_name']; $row['bloc_name'] = $val['bloc_name']; $row['dealer_name'] = $val['dealer_name']; $row['type'] = $val['type']; $row['garment'] = $val['garment']; $row['garment_function'] = $val['garment_function']; $row['clothing'] = $val['clothing']; $row['clothing_function'] = $val['clothing_function']; $row['functional_finishing'] = $val['functional_finishing']; $row['create_time'] = date('Y-m-d',$val['create_time']); $csv_data .= join(',', array_values($row))." "; } $file_name = date('YmdHis', time()).'detail.csv'; //导出数据 $url = save_csv($file_name, $csv_data); $this->success('成功!','',$url); exit();
无论导出多大的数据,都不会出现乱码
人生得意须尽欢,莫使金樽空对月.
-----------------------------------------------------------------------------自己项目实战------------------------------------------------------------------------------------------------------------
注意:请求方法必须是get请求,不能用post请求
//用户提现审核导出
public function check_daochu()
{
$req = Db::name('cash_out')->alias('c')
->field('ca.card_name,ca.card_no,c.add_time,c.actual_money')
->join('xcf_user_wallet uw', 'uw.user_id=c.user_id', 'left')
->join('xcf_cash_account ca', "ca.id=c.account_id", "left")
->join('xcf_users u', "u.user_id=c.user_id", "left")
->where('c.role_id', 1)
->where('c.status', 1)
->order('c.add_time desc')
->select();
// ["card_name"] => string(6) "王毅"
// ["card_no"] => string(11) "15327953555"
// ["add_time"] => string(19) "2019-10-22 15:46:34"
// ["actual_money"] => string(4) "2.47"
$csv_data = '';
//标题
$csv_data .= "姓名,支付宝账号,实际到账金额,提现时间
";
$row = array();
foreach ($req as $k=>$val){
$row['store_name'] = $val['card_name'];
$row['bloc_name'] = $val['card_no'];
$row['dealer_name'] = $val['actual_money'];
$row['create_time'] =$val['add_time'];
$csv_data .= join(',', array_values($row))."
";
}
$file_name = date('Y-m-d H-i-s', time()).' 用户提现.csv';
//导出数据
$url = $this->csvExportWithBOM($file_name, $csv_data);
exit();
//$this->success('成功!','',$url);
// return $url;
//return json(['ok' => 'success']);
}
/**@param $filename
* 输出csv文件的文件名
* @param $data
* 要写入csv文件的内容,即经过csvGenerator处理过的字符串;
*/
public function csvExportWithBOM($filename='export.csv', $data)
{
$f=fopen("php://memory",'w');
fwrite($f, chr(0xEF).chr(0xBB).chr(0xBF).$data);//加入BOM头
fseek($f,0);
header('Content-Type: application/csv');
// 告知浏览器想要输出的是一个文件
header('Content-Disposition: attachment; filename="'.$filename.'";');
// make php send the generated csv lines to the browser
fpassthru($f);
}