作者 开飞机的舒克

新增后台导出功能

@@ -51,15 +51,15 @@ class Study extends Backend @@ -51,15 +51,15 @@ class Study extends Backend
51 list($where, $sort, $order, $offset, $limit) = $this->buildparams(); 51 list($where, $sort, $order, $offset, $limit) = $this->buildparams();
52 52
53 $list = $this->model 53 $list = $this->model
54 - ->with(['school','team'])  
55 - ->where($where)  
56 - ->order($sort, $order)  
57 - ->paginate($limit); 54 + ->with(['school', 'team'])
  55 + ->where($where)
  56 + ->order($sort, $order)
  57 + ->paginate($limit);
58 58
59 foreach ($list as $row) { 59 foreach ($list as $row) {
60 - 60 +
61 $row->getRelation('school')->visible(['title']); 61 $row->getRelation('school')->visible(['title']);
62 - $row->getRelation('team')->visible(['title']); 62 + $row->getRelation('team')->visible(['title']);
63 } 63 }
64 64
65 $result = array("total" => $list->total(), "rows" => $list->items()); 65 $result = array("total" => $list->total(), "rows" => $list->items());
@@ -70,65 +70,76 @@ class Study extends Backend @@ -70,65 +70,76 @@ class Study extends Backend
70 } 70 }
71 71
72 //导出数据 72 //导出数据
73 - public function export(){ 73 + public function export()
  74 + {
74 if ($this->request->isPost()) { 75 if ($this->request->isPost()) {
75 set_time_limit(0); 76 set_time_limit(0);
76 $ids = $this->request->post('ids'); 77 $ids = $this->request->post('ids');
77 78
78 $excel = new \PHPExcel(); 79 $excel = new \PHPExcel();
79 - $excel->getProperties()->setTitle("学校+积分表");  
80 - $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);  
81 - $excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);  
82 - $excel->getActiveSheet()->getColumnDimension('C')->setWidth(10);  
83 - $excel->getActiveSheet()->getColumnDimension('D')->setWidth(10);  
84 - $excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);  
85 - $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);  
86 - $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);  
87 - $excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);  
88 - // 开始操作excel表  
89 - $worksheet = $excel->setActiveSheetIndex(0) //操作第一个工作表  
90 - ->setCellValue('A1','序号') //设置表格标题名称  
91 - ->setCellValue('B1','姓名') //设置表格标题名称  
92 - ->setCellValue('C1','性别')  
93 - ->setCellValue('D1','班级')  
94 - ->setCellValue('E1','学校')  
95 - ->setCellValue('F1','手环ID')  
96 - ->setCellValue('G1','学号')  
97 - ->setCellValue('H1','总积分');  
98 - if ($ids=='all' || empty($ids)){ //判断当前数据为全部或者选中的  
99 - $list = $this->model->with('school')->select();  
100 - }else{  
101 - $list = $this->model->with('school')->select($ids); 80 + try {
  81 + $excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
  82 + $excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
  83 + $excel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
  84 + $excel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
  85 + $excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
  86 + $excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  87 + $excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
  88 + $excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
  89 +
  90 + // 列名表头文字加粗
  91 + $excel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true);
  92 + // 列表头文字居中
  93 + $excel->getActiveSheet()->getStyle('A1:J1')->getAlignment()
  94 + ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  95 +
  96 + // 开始操作excel表
  97 + $worksheet = $excel->setActiveSheetIndex(0) //操作第一个工作表
  98 + ->setCellValue('A1', '序号') //设置表格标题名称
  99 + ->setCellValue('B1', '姓名') //设置表格标题名称
  100 + ->setCellValue('C1', '性别')
  101 + ->setCellValue('D1', '班级')
  102 + ->setCellValue('E1', '学校')
  103 + ->setCellValue('F1', '手环ID')
  104 + ->setCellValue('G1', '学号')
  105 + ->setCellValue('H1', '总积分');
  106 + if ($ids == 'all' || empty($ids)) { //判断当前数据为全部或者选中的
  107 + $list = $this->model->with('school')->select();
  108 + } else {
  109 + $list = $this->model->with('school')->select($ids);
  110 + }
  111 +
  112 + foreach ($list as $k => $val) {
  113 + $kk = $k + 1;
  114 + $k = $k + 2;//表格是从2开始的
  115 + $excel->getActiveSheet()->setTitle($val['school']['title'] . '积分表');
  116 + $worksheet
  117 + ->setCellValue('A' . $k, $val['id'])
  118 + ->setCellValue('B' . $k, $val['name'])
  119 + ->setCellValue('C' . $k, $val['gender'] ? '男' : '女')
  120 + ->setCellValue('D' . $k, $val['grade'])
  121 + ->setCellValue('E' . $k, $val['school']['title'])
  122 + ->setCellValue('F' . $k, $val['unique'])
  123 + ->setCellValue('G' . $k, $val['sno'])
  124 + ->setCellValue('H' . $k, $val['earn_score']);
  125 + }
  126 + $excel->createSheet();
  127 + $title = "学生成绩" . date("YmdHis");
  128 + header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  129 + header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
  130 + header('Cache-Control: max-age=0');
  131 + header('Cache-Control: max-age=1');
  132 +
  133 + header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  134 + header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  135 + header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  136 + header('Pragma: public'); // HTTP/1.0
  137 + $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  138 + $objWriter->save('php://output');
  139 + } catch (\PHPExcel_Exception $e) {
  140 + $this->error('参数错误', $e->getMessage());
102 } 141 }
103 -  
104 - foreach ($list as $k => $val){  
105 - $k = $k + 2;//表格是从2开始的  
106 - $worksheet  
107 - ->setCellValue('A'.$k,$val['id'])  
108 - ->setCellValue('B'.$k,$val['name'])  
109 - ->setCellValue('C'.$k,$val['gender']?'男':'女')  
110 - ->setCellValue('D'.$k,$val['grade'])  
111 - ->setCellValue('E'.$k,$val['school']['title'])  
112 - ->setCellValue('F'.$k,$val['unique'])  
113 - ->setCellValue('G'.$k,$val['sno'])  
114 - ->setCellValue('H'.$k,$val['earn_score']);  
115 - }  
116 -  
117 - $excel->createSheet();  
118 - $title = "学生成绩记录" . date("YmdHis");  
119 - header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  
120 - header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');  
121 - header('Cache-Control: max-age=0');  
122 - header('Cache-Control: max-age=1');  
123 -  
124 - header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past  
125 - header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified  
126 - header('Cache-Control: cache, must-revalidate'); // HTTP/1.1  
127 - header('Pragma: public'); // HTTP/1.0  
128 - $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');  
129 - $objWriter->save('php://output');  
130 exit; 142 exit;
131 - return;  
132 } 143 }
133 } 144 }
134 } 145 }