1.1前端界面(模式 1)!--html--formclasslayui-form layui-form-pane form-saveactiondivclasslayui-col-xs3 layui-col-sm3 layui-col-md3styletextareanameprovider_order_idclasslayui-input/textarea/divbuttonclasslayui-btn layui-btn-primary layui-btn-mdlay-submitsearchlay-filtersearchidsearchdata-typesearch查询/buttonbuttonclasslayui-btn layui-btn-danger layui-btn-mdlay-submitexportlay-filterexportidexportdata-typeexport导出/button/form// 表单提交form.on(submit(export),function(data){varparamesnewArray();parames.push({name:data,value:JSON.stringify(data.field)});Post(?php echo Url::toRoute(url);?,parames);returnfalse;});// 跳转后端接口functionPost(URL,PARAMTERS){//创建form表单vartemp_formdocument.createElement(form);temp_form.actionURL;//如需打开新窗口form的target属性要设置为_blanktemp_form.target_blank;temp_form.methodpost;temp_form.style.displaynone;//添加参数for(variteminPARAMTERS){varoptdocument.createElement(textarea);opt.namePARAMTERS[item].name;opt.valuePARAMTERS[item].value;temp_form.appendChild(opt);}document.body.appendChild(temp_form);//提交数据temp_form.submit();}1.2前端界面(模式 2)// 1.导出按钮script typetext/htmlidtable-toolbarbuttonclasspear-btn pear-btn-primary pear-btn-mdlay-eventexport导出/button/script// 2.表关联按钮table.render({method:post,limit:10,id:table,elem:#table,toolbar:#table-toolbar,cols:[[//表头{checkbox:true},]],done:function(res,curr,count){}});// 3.监听表的点击事件table.on(toolbar(table),function(obj){if(obj.eventexport){varcheckStatustable.checkStatus(obj.config.id);varids[];datacheckStatus.data;for(vari0;idata.length;i){ids.push(data[i].id);}Post(?php echo Url::toRoute(export);?,ids);returnfalse;}});// 4.提交数据至后端functionPost(URL,PARAMTERS){//创建form表单vartemp_formdocument.createElement(form);temp_form.actionURL;//如需打开新窗口form的target属性要设置为_blanktemp_form.target_blank;temp_form.methodpost;temp_form.style.displaynone;varoptdocument.createElement(input);opt.namename;opt.valuePARAMTERS;temp_form.appendChild(opt);document.body.appendChild(temp_form);//提交数据temp_form.submit();}2.后端业务/** * 接受查询条件和查询 * */publicfunctionactionExport(){if(Yii::$app-request-isPost){$postYii::$app-request-post(data);$postjson_decode($post,true);$data1$this-getdata($post);foreach($data1[data]as$k$d){$data[$k][provider_order_id]$d[provider_order_id];// ..}$this-xlsWriteExcel(orderNum,value,订单号,订单价格,$data);}}/** * 插件导出 * */functionxlsWriteExcel($field,$header,$data,$fileName自定义导出){require_onceYii::getAlias(vendor)./phpxlsxwriter/vendor/autoload.php;header(Content-Type: text/html;charsetutf-8);error_reporting(E_ALL);set_time_limit(0);ob_start();$sheetSheet1;$writernew\XLSXWriter();$fieldexplode(,,$field);$headerexplode(,,$header);$headerarray_fill_keys($header,string);$styles1array(font宋体,font-size10,font-stylebold,fill#eee,haligncenter,borderleft,right,top,bottom);$titlearray_keys($header);$writer-writeSheetRow($sheet,$title,$styles1);if(!empty($data)){foreach($dataas$k$v){foreach($fieldas$k1$v1){if(isset($v[$v1])){$rows[$k1]$v[$v1]. ;}else{$rows[$k1];}}$writer-writeSheetRow($sheet,$rows,$styles1);}}$filename$fileName.date(YmdHis,time())..xlsx;$tempFiletempnam(sys_get_temp_dir(),excel);$writer-writeToFile($tempFile);header(Content-Type: application/octet-stream);header(Content-Disposition: attachment; filename.$filename);readfile($tempFile);unlink($tempFile);}