html代码
<a href="javascript:;" class="btn btn-success" id="excel_dc"> {:__('导出')}</a>
js代码
// 导出数据
$('#excel_dc').click(function(){
var filter='{';
var op='{';
// 跳转
var formdata = $("form.form-commonsearch").serialize();
var data=formdata.split('&');
data.forEach(function(v){
let a= v.split('=');
if(a[1]){
filter+='"'+[a[0]]+'":"'+a[1]+'",';
op+='"'+[a[0]]+'":"",';
}
})
if(filter.length > 2){
filter=filter.slice(0,-1);
op=op.slice(0,-1);
}
var url='http://www.aaa.cn/aaaaa.php/aaa/aaa/excel_dc?addtabs=1&sort=id&order=desc&offset=0&limit=18000&filter=';
url += filter +'}&op='+ op+'}';
window.open(url);
})
PHP代码
/**
* 数据导出
*/
public function excel_dc(){
$ck=array_column($ck,null,'id');
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->where($where)
->order($sort, $order)
->paginate($limit);
$list=$list->toArray();
// var_dump($count);die;
# 实例化phpexcel
vendor("PHP_XLSXWriter.XLSXWriter");
$writer = new \XLSXWriter;
$sheetHeaderb=['会员名称','会员电话','会员性别','会员年龄'];
$writer->writeSheetRow('Sheet1', $sheetHeaderb);
# 数据填充
foreach ($list['data'] as $k => $v) {
$writer->writeSheetRow('Sheet1', array($v['name'],$v['sj'],$v['xb'],$v['nl']) );
}
$fileName=date('Y-m-d');
//设置 header,用于浏览器下载
$writer->writeToStdOut();
header('Content-disposition: attachment; filename="'.$fileName.'.xlsx'.'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}