model = new \app\common\model\user4s\Inslog; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { $all_star = array( 1=>'★', 2=>'★★', 3=>'★★★', 4=>'★★★★', 5=>'★★★★★', ); //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['admin','user']) ->where($where) ->order($sort, $order) ->paginate($limit); foreach ($list as &$item) { $item->star_f = $all_star[$item->star]; $item->createtime_f = date('Y-m-d',$item->createtime); } $result = array("total" => $list->total(), "rows" => $list->items(), "extend" => ['start_data'=>date('Y-m-d',strtotime('-1 month')),'end_data'=>date('Y-m-d')]); return json($result); } return $this->view->fetch(); } /** * 对Spreadsheet方法封装(锐庆) * @param array $arr 该数组必须为键值对,键是表格单元,值是单元格的值 * @param array $data 该数组如果为一维数组,则填写一行,如果为二维数组,则多行数据 * @param string $name 下载Excel的文件名,可忽略 * @param string $type 选择文件类型,如果不填写,默认下载为xlsx格式,如果任意填写数值为xls格式 * @param int $with 设置sheet默认列宽 */ public function downloadExcel(array $arr, array $data, $name = "", $type = "Xlsx") { //文件名处置 if (empty($name)) { $name = date("Y-m-d H:i:s") . "_" . rand(1000, 9999); } else { $name = $name . "_" . date("Y-m-d H:i:s"); } //内容设置 $preadsheet = new Spreadsheet(); $sheet = $preadsheet->getActiveSheet(); foreach ($arr as $k => $v) { $sheet->setCellValue($k, $v); } $sheet->fromArray($data, null, "A2"); //样式设置 $sheet->getDefaultColumnDimension()->setWidth(20); //设置下载与后缀 if ($type == "Xlsx") { header("Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); $suffix = "xlsx"; } else { header("Content-Type:application/vnd.ms-excel"); $type = "Xls"; $suffix = "xls"; } header("Content-Disposition:attachment;filename=$name.$suffix"); header("Cache-Control:max-age=0"); //缓存控制 $writer = IOFactory::createWriter($preadsheet, $type); $writer->save("php://output"); //数据流 } public function excel() { set_time_limit(0); $all_star = array( 1 => '★', 2 => '★★', 3 => '★★★', 4 => '★★★★', 5 => '★★★★★', ); //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); $start_date = $this->request->get("start_date", ''); $end_date = $this->request->get("end_date", ''); if(empty($start_date) || empty($end_date)){ $this->error('请选择要导出的日期范围'); } $ins_where = array('inslog.insdate' => ['between', [date('Y-m-d', strtotime($start_date)), date('Y-m-d', strtotime($end_date))]]); $data = array(); $head = array( 'A1' => '编号', 'B1' => '姓名', 'C1' => '联系方式', 'D1' => '身份证', 'E1' => '生日', 'F1' => '购车日期', 'G1' => '车牌号码', 'H1' => '车架号', 'I1' => '车型', 'J1' => '颜色', 'K1' => '续保保险类型', 'L1' => '续保保险公司', 'M1' => '续保日期', 'N1' => '满意度', 'O1' => '备注', ); $list = $this->model->with(['user'])->where($ins_where)->select(); //var_dump($this->model->getLastSql());exit(); $logs = collection($list)->toArray(); foreach ($logs as $index => $item) { //var_dump($item); $data[] = array( $item['id'], $item['user']['name'], ' '.$item['user']['tel'], ' '.$item['user']['cardid'], date('Y年m月d日',strtotime($item['user']['birthday'])), date('Y年m月d日',strtotime($item['user']['buydate'])), $item['user']['carno'], $item['user']['frameno'], $item['user']['model'], $item['user']['color'], $item['instype'], $item['inscom'], date('Y年m月d日',strtotime($item['insdate'])), $all_star[$item['star']], $item['description'], ); } return $this->downloadExcel($head,$data); } }