作者 开飞机的舒克

新增后台导出功能

... ... @@ -51,15 +51,15 @@ class Study extends Backend
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$list = $this->model
->with(['school','team'])
->where($where)
->order($sort, $order)
->paginate($limit);
->with(['school', 'team'])
->where($where)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('school')->visible(['title']);
$row->getRelation('team')->visible(['title']);
$row->getRelation('team')->visible(['title']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
... ... @@ -70,65 +70,76 @@ class Study extends Backend
}
//导出数据
public function export(){
public function export()
{
if ($this->request->isPost()) {
set_time_limit(0);
$ids = $this->request->post('ids');
$excel = new \PHPExcel();
$excel->getProperties()->setTitle("学校+积分表");
$excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
// 开始操作excel表
$worksheet = $excel->setActiveSheetIndex(0) //操作第一个工作表
->setCellValue('A1','序号') //设置表格标题名称
->setCellValue('B1','姓名') //设置表格标题名称
->setCellValue('C1','性别')
->setCellValue('D1','班级')
->setCellValue('E1','学校')
->setCellValue('F1','手环ID')
->setCellValue('G1','学号')
->setCellValue('H1','总积分');
if ($ids=='all' || empty($ids)){ //判断当前数据为全部或者选中的
$list = $this->model->with('school')->select();
}else{
$list = $this->model->with('school')->select($ids);
try {
$excel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$excel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$excel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$excel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
$excel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
// 列名表头文字加粗
$excel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true);
// 列表头文字居中
$excel->getActiveSheet()->getStyle('A1:J1')->getAlignment()
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 开始操作excel表
$worksheet = $excel->setActiveSheetIndex(0) //操作第一个工作表
->setCellValue('A1', '序号') //设置表格标题名称
->setCellValue('B1', '姓名') //设置表格标题名称
->setCellValue('C1', '性别')
->setCellValue('D1', '班级')
->setCellValue('E1', '学校')
->setCellValue('F1', '手环ID')
->setCellValue('G1', '学号')
->setCellValue('H1', '总积分');
if ($ids == 'all' || empty($ids)) { //判断当前数据为全部或者选中的
$list = $this->model->with('school')->select();
} else {
$list = $this->model->with('school')->select($ids);
}
foreach ($list as $k => $val) {
$kk = $k + 1;
$k = $k + 2;//表格是从2开始的
$excel->getActiveSheet()->setTitle($val['school']['title'] . '积分表');
$worksheet
->setCellValue('A' . $k, $val['id'])
->setCellValue('B' . $k, $val['name'])
->setCellValue('C' . $k, $val['gender'] ? '男' : '女')
->setCellValue('D' . $k, $val['grade'])
->setCellValue('E' . $k, $val['school']['title'])
->setCellValue('F' . $k, $val['unique'])
->setCellValue('G' . $k, $val['sno'])
->setCellValue('H' . $k, $val['earn_score']);
}
$excel->createSheet();
$title = "学生成绩" . date("YmdHis");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');
} catch (\PHPExcel_Exception $e) {
$this->error('参数错误', $e->getMessage());
}
foreach ($list as $k => $val){
$k = $k + 2;//表格是从2开始的
$worksheet
->setCellValue('A'.$k,$val['id'])
->setCellValue('B'.$k,$val['name'])
->setCellValue('C'.$k,$val['gender']?'男':'女')
->setCellValue('D'.$k,$val['grade'])
->setCellValue('E'.$k,$val['school']['title'])
->setCellValue('F'.$k,$val['unique'])
->setCellValue('G'.$k,$val['sno'])
->setCellValue('H'.$k,$val['earn_score']);
}
$excel->createSheet();
$title = "学生成绩记录" . date("YmdHis");
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $title . '.xlsx"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output');
exit;
return;
}
}
}
... ...