15年软件开发经验,只做源码定制!

原创设计 定制开发

满足您的个性化需求

当前位置:首页 前端开发 H5

数据库导出xls格式表

陈敏| 发布于 2021-10-28 08:50:10| 91阅读| 0点赞| 0评论
举报

使用第三方内库实现数据库导出


 public function orderDaochu()
    {
        $where=[];
        $post = $this->request->post();     //年月日判断  
        if(isset($post['create_time']) and !empty($post['create_time'])) {
            $min_time = strtotime($post['create_time']);
            $max_time = $min_time + 24 * 60 * 60;
            $where['create_time'] = [['>=',$min_time],['<=',$max_time]];
        }
        $model = new userModel();
        $address = $model->where($where)->select();
        vendor("PHPExcel.PHPExcel.PHPExcel");//引入插件
        $objPHPExcel = new \PHPExcel();
        // 设置sheet
        $objPHPExcel->setActiveSheetIndex(0);
        // 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
        // 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'ID');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '昵称');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '用户头像');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '性别');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '年龄');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '积分');
        $objPHPExcel->getActiveSheet()->SetCellValue('G1', '添加用户时间');
        $objPHPExcel->getActiveSheet()->SetCellValue('H1', '手机号');
        $objPHPExcel->getActiveSheet()->SetCellValue('I1', '用户名');
        $objPHPExcel->getActiveSheet()->SetCellValue('J1', '收货人手机号');
        $objPHPExcel->getActiveSheet()->SetCellValue('K1', '地址');
        $objPHPExcel->getActiveSheet()->SetCellValue('L1', '生日');
        //存取数据
        $num = 2;
        foreach ($address as $k => $v) {        //你的数据库内容
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, $v['id']);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['nickname']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['thumb']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['sex']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['age']);
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, $v['integral']);
            $objPHPExcel->getActiveSheet()->SetCellValue('G' . $num, $v['create_time']);
            $objPHPExcel->getActiveSheet()->SetCellValue('H' . $num, $v['phone']);
            $objPHPExcel->getActiveSheet()->SetCellValue('I' . $num, $v['shou_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('J' . $num, $v['shou_phone']);
            $objPHPExcel->getActiveSheet()->SetCellValue('K' . $num, $v['address']);
            $objPHPExcel->getActiveSheet()->SetCellValue('L' . $num, $v['user_time']);

            $num++;
        }
        // 文件名称
        $fileName = "用户" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        // 设置工作表名
        $objPHPExcel->getActiveSheet()->setTitle('sheet');
        //下载 excel5与excel2007
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
        ob_end_clean();     // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
        return json(['msg'=>'导出成功']);
    }
0

0条评论

别默默看啦~登录/注册一起参与讨论吧~

热门标签

陈敏
微信扫一扫立即咨询
账号登录|扫码登录

立即注册 |忘记密码?

欢迎注册

已有账号马上登录

重置密码

扫码绑定微信
微信扫一扫

绑定手机号

分享到-微信

举报

  • 举报类型:

  • 举报描述:

您好,当前积分不足。

在线客服
拨打电话
17330196230 13230981129
顶部