Layui表单查询导出

时间:2024-10-02 22:10:17
/** * 接受查询条件和查询 * */ public function actionExport(){ if (Yii::$app->request->isPost){ $post = Yii::$app->request->post('data'); $post = json_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); } } /** * 插件导出 * */ function xlsWriteExcel($field,$header,$data,$fileName='自定义导出'){ require_once Yii::getAlias('@vendor') . '/phpxlsxwriter/vendor/autoload.php'; header("Content-Type: text/html;charset=utf-8"); error_reporting(E_ALL); set_time_limit(0); ob_start(); $sheet = 'Sheet1'; $writer = new \XLSXWriter(); $field = explode(',',$field); $header = explode(',',$header); $header = array_fill_keys($header,"string"); $styles1 = array( 'font'=>'宋体','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center','border'=>'left,right,top,bottom' ); $title = array_keys($header); $writer->writeSheetRow($sheet, $title, $styles1); if(!empty($data)){ foreach ($data as $k=>$v){ foreach ($field as $k1=>$v1){ if(isset($v[$v1])){ $rows[$k1] = $v[$v1].' '; }else{ $rows[$k1] = ''; } } $writer->writeSheetRow($sheet, $rows, $styles1); } } $filename = $fileName.date('YmdHis',time()).'.xlsx'; $tempFile = tempnam(sys_get_temp_dir(), 'excel'); $writer->writeToFile($tempFile); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . $filename); readfile($tempFile); unlink($tempFile); }