我引用的拓展是:
"phpoffice/phpspreadsheet": "^1.4",
我的$accept数据是这样的 "data": {
"examStu": [
{
"ctime": "2023-04-27 16:28:16",
"className": "大猛1",
"no": "sy_stud",
"name": "小明",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 16:28:16",
"className": "非正常人类研究中心",
"no": "sy_student2",
"name": "试用学生2",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 16:28:16",
"className": "非正常人类研究中心",
"no": "sy_student1",
"name": "试用学生1",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 137,
"isSubmit": true,
"status": 1,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 16:28:16",
"className": "非正常人类研究中心",
"no": "9987234761123",
"name": "范仲淹AAA",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 16:28:16",
"className": "非正常人类研究中心",
"no": "sy_student3",
"name": "试用学生3",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 16:28:16",
"className": "非正常人类研究中心",
"no": "131",
"name": "学生2",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 16:28:16",
"className": "非正常人类研究中心",
"no": "130",
"name": "学生3",
"submitExam": "2023-04-27 16:28:16",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
},
{
"ctime": "2023-04-27 20:18:54",
"className": "非正常人类研究中心",
"no": "133",
"name": "学生1",
"submitExam": "2023-04-27 20:18:54",
"stuExamId": 0,
"isSubmit": false,
"status": 0,
"realScore": 0,
"hundredScore": 0
}
],
"scoreInfo": [
{
"sixScore": 1,
"sevenScore": 0,
"eightScore": 0,
"nineScore": 0,
"tenScore": 0,
"classes": "192",
"className": "非正常人类研究中心"
},
{
"sixScore": 0,
"sevenScore": 0,
"eightScore": 0,
"nineScore": 0,
"tenScore": 0,
"classes": "195",
"className": "大猛1"
}
],
"examInfo": {
"passRate": 0,
"maxRate": 0,
"minRate": 0,
"middleRate": 0,
"avgRate": 0
}
}
具体导出代码是这样的:
$accept = self::analyseExam($datas); $title = array('加入时间', '班级', '学号', '姓名', '交卷时间', '学生交卷序号(未交卷为0)', '提交状态', '批改状态', '实际得分', '百分制得分'); $data = $accept['examStu']; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet()->setTitle('sheet1'); $sheet_two = $spreadsheet->createSheet(2)->setTitle('sheet2'); $writer = new Xlsx($spreadsheet); //表头 //设置单元格内容 foreach ($title as $key => $value) { $worksheet->setCellValueByColumnAndRow($key + 1, 1, $value); } $twoTitle = ['及格率', '最高分', '最低分', '中位分', '平均分']; foreach ($twoTitle as $k => $v) { $sheet_two->setCellValueByColumnAndRow($k + 1, 1, $v); } $row = 2; //第二行开始 $data = $data->toArray(); foreach ($data as $key => &$value) { // $data[$key]['incId'] = $key+1; if ($value['isSubmit'] == false) { $value['isSubmit'] = '未提交'; } else { $value['isSubmit'] = '已提交'; } if ($value['status'] == false || $value['status'] == 1) { $value['status'] = '未批改'; } elseif ($value['status'] == 2) { $value['status'] = '已批改'; } } // exit(); foreach ($data as $item) { $column = 1; foreach ($item as $value) { $worksheet->setCellValueByColumnAndRow($column, $row, $value); $column++; } $row++; } $twoData = $accept['examInfo']; $row = 2; //第二行开始 foreach ($twoData as $items) { $column = 1; $sheet_two->setCellValueByColumnAndRow($column, $row, $items); $sheet_two->setCellValueByColumnAndRow($column + 1, $row, $items); $sheet_two->setCellValueByColumnAndRow($column + 2, $row, $items); $sheet_two->setCellValueByColumnAndRow($column + 3, $row, $items); $sheet_two->setCellValueByColumnAndRow($column + 4, $row, $items); } # 保存为xlsx $filename = 'TESTExcel.xlsx'; $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save($filename); # 浏览器下载 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); return $writer->save('php://output');
总结:核心多个sheet代码为:
$spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet()->setTitle('sheet1'); $sheet_two = $spreadsheet->createSheet(2)->setTitle('sheet2');
其他代码为渲染数据进excel代码 这里不做细谈
一起看看效果: