html:
<span class="layui-btn layui-btn-primary export" id="export">导出数据</span>
js:
//导出操作
$("#export").click(function (){
//弹出加载层
var load = layer.load();
var value = getFormJson($("#form"));
$.post('<?= Url::to(['hospital/export']) ?>', value, function (res){
if(typeof res == "string"){
res = $.parseJSON(res);
}
if(res.status == 1){
layer.open({
title: '导出excel',
content: '生成excel文件成功!',
btn:['下载'],
yes:function(index, layero){
window.location.href= res.data.file_url;
layer.close(index);
}
});
}else{
layer.alert(res.msg, {icon: 2, time:3000});
}
layer.close(load);
});
});
php代码:
public function actionExport()
{
//设置最大执行时间
ini_set("max_execution_time", "720");
ini_set('memory_limit', '512M');
$request = Yii::$app->request;
$params = $request->post();
$model = new OutpatientAppointmentOrder();
$where = [];
if (isset($params['order_no']) && !empty($params['order_no'])) {
$where[] = ['=', 'order_no', trim($params['order_no'])];
}
if (isset($params['create_time']) && !empty($params['create_time'])) {
$tmp_arr = explode(' - ', trim($params['create_time']));
$begin = $tmp_arr[0]. '00:00:00';
$end = $tmp_arr[1]. '23:59:59';
$where[] = ['>=', 'create_time', strtotime($begin)];
$where[] = ['<=', 'create_time', strtotime($end)];
}
if (isset($params['mobile']) && !empty($params['mobile'])) {
$where[] = ['=', 'mobile', trim($params['mobile'])];
}
if (isset($params['expect_hospital']) && !empty($params['expect_hospital'])) {
$where[] = ['like', 'expect_hospital', trim($params['expect_hospital'])];
}
if (isset($params['expect_department']) && !empty($params['expect_department'])) {
$where[] = ['like', 'expect_department', trim($params['expect_department'])];
}
if (isset($params['status']) && !empty($params['status'])) {
$where[] = ['=', 'status', intval($params['status'])];
}
if (isset($params['name']) && !empty($params['name'])) {
$where[] = ['like', 'name', trim($params['name'])];
}
if (isset($params['id_card_no']) && !empty($params['id_card_no'])) {
$where[] = ['=', 'id_card_no', trim($params['id_card_no'])];
}
$data = $model->getExportData($where);
//导出文件名
$fileName = date('YmdHis') . '_outpatient_appointment_order_list';
$relative_path = 'outpatient-appointment-order/excel/' . date('Y') . '/' . date('m') . '/' . date('d') . '/';
$absolute_path = Yii::getAlias('@webroot') . '/upload/' . $relative_path;
if (!file_exists($absolute_path)) {
try {
FileHelper::createDirectory($absolute_path, 0777, true);
} catch (\Exception $e) {
return ['code' => 400, 'msg' => '目录创建失败,' . $e->getMessage()];
}
}
//导出设置
Excel::widget([
'models' => $data,
'fileName' => $fileName,
'mode' => 'export',
'headers' => [
'id' => 'ID',
'create_time' => '申请时间',
'name' => '姓名',
'mobile' => '手机号',
'id_card_no' => '身份证号',
'expect_hospital' => '期望医院',
'expect_department' => '期望科室',
'expect_time' => '期望时间',
],
'columns' => [
'id',
[
'attribute' => 'create_time',
'format' => 'text',
'value' => function ($model) {
return $model['create_time'] > 0 ? date('Y-m-d H:i:s', $model['create_time']) : '无';
}
],
'name',
'mobile',
'id_card_no',
'expect_hospital',
'expect_department',
'expect_time',
],
'asArray' => true,
'savePath' => $absolute_path,
'asAttachment' => false,
]);
$fileUrl = rtrim('http://'.Yii::$app->request->getHostName(), '/') . '/upload/'. trim($relative_path, '/') . '/' . $fileName . '.xlsx';
return $this->jsonSuccess(['file_url' => $fileUrl], '导出成功');
}
最终实现效果