php数据导出功能

5 篇文章 0 订阅
5 篇文章 0 订阅

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], '导出成功');
    }

最终实现效果

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PHP开光程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值