Exams.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\admin\model\department\Department;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\teacher\ExamsScore;
  9. use app\admin\model\Fault;
  10. use app\admin\model\Report;
  11. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  12. use PhpOffice\PhpSpreadsheet\IOFactory;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  16. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  17. /**
  18. * sim-考试表/成绩总分
  19. *
  20. * @icon fa fa-circle-o
  21. */
  22. class Exams extends Backend
  23. {
  24. /**
  25. * Exams模型对象
  26. * @var \app\admin\model\teacher\Exams
  27. */
  28. protected $model = null;
  29. protected $whereExtend = null;
  30. protected $noNeedRight = ['export'];
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model = new \app\admin\model\teacher\Exams;
  35. $this->whereExtend['exam_collection_type'] = 3;
  36. $this->whereExtend['starttime'] = ['>',0];
  37. $this->whereExtend['endtime'] = ['>',0];
  38. $this->relationtTable = 'collection';
  39. }
  40. /**
  41. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  42. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  43. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  44. */
  45. public function index()
  46. {
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if (false === $this->request->isAjax()) {
  50. return $this->view->fetch();
  51. }
  52. //如果发送的来源是 Selectpage,则转发到 Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  57. $list = $this->model
  58. ->where($where)->where($this->whereExtend)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. //最高分 最低分 平均分
  62. $select = $this->model->where($where)->where($this->whereExtend)->field('count(*) as count,sum(total_score) as totalscore,min(total_score) as min,max(total_score) as max')->select();
  63. $pingfen = '';
  64. if(!empty($select[0]['totalscore']) && !empty($select[0]['count'])){
  65. $pingfen = bcdiv($select[0]['totalscore'],$select[0]['count']);
  66. }
  67. $defen = ['max'=>$select[0]['max'],'min'=>$select[0]['min'],'pingfen'=>$pingfen];
  68. $one_static = [];
  69. $two_static = [];
  70. $three_static = [];
  71. $where_falut = ['fault_type' => 3,'fault_state'=>0];
  72. $exam_ids = $this->model->where($where)->where($this->whereExtend)->column('exam_id');
  73. foreach ($list as $k => $val){
  74. $one = Fault::where(['sim_type'=>'0001'])->where($where_falut)->field('fault_id,name')->select();
  75. if($val['sim_type']=='0001'){
  76. foreach ($one as $k1 => $val1){
  77. //先查分配次数,在查错误率
  78. $one_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id']])->count();
  79. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val1['fault_id'],'answer_right'=>2])->count();
  80. $one[$k1]['epercent'] = '';
  81. if($one_total>0){
  82. $bili = bcmul(bcdiv($error_total,$one_total,2),100);
  83. $one[$k1]['epercent'] = $bili>0 ? $bili.'%' : '';
  84. }
  85. }
  86. $vaccineCount = array_column($one, 'epercent');
  87. array_multisort($vaccineCount,SORT_DESC,$one);
  88. $one_static = $one;
  89. }
  90. $two = Fault::where(['sim_type'=>'0002'])->where($where_falut)->field('fault_id,name')->select();
  91. if($val['sim_type']=='0002'){
  92. foreach ($two as $k2 => $val2){
  93. $two_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id']])->count();
  94. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val2['fault_id'],'answer_right'=>2])->count();
  95. $two[$k2]['epercent'] = '';
  96. if($two_total>0){
  97. $bili = bcmul(bcdiv($error_total,$two_total,2),100);
  98. $two[$k2]['epercent'] = $bili>0 ? $bili.'%' : '';
  99. }
  100. }
  101. $vaccineCount1 = array_column($two, 'epercent');
  102. array_multisort($vaccineCount1,SORT_DESC,$two);
  103. }
  104. $two_static = $two;
  105. $three = Fault::where(['sim_type'=>'0003'])->where($where_falut)->field('fault_id,name')->select();
  106. if($val['sim_type']=='0003'){
  107. foreach ($three as $k3 => $val3){
  108. $three_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id']])->count();
  109. $error_total = Db::name('real_exam_fault')->where(['flag'=>1,'exam_id'=>['in',$exam_ids],'fault_id'=>$val3['fault_id'],'answer_right'=>2])->count();
  110. $three[$k3]['epercent'] = '';
  111. if($three_total>0){
  112. $bili = bcmul(bcdiv($error_total,$three_total,2),100);
  113. $three[$k3]['epercent'] = $bili>0 ? $bili.'%' : '';
  114. }
  115. }
  116. $vaccineCount2 = array_column($three, 'epercent');
  117. array_multisort($vaccineCount2,SORT_DESC,$three);
  118. }
  119. $three_static = $three;
  120. }
  121. //统计错误率
  122. $statistics = [
  123. 'one_static' => $one_static,
  124. 'two_static' => $two_static,
  125. 'three_static' => $three_static,
  126. ];
  127. $result = ['total' => $list->total(), 'rows' => $list->items(),'defen'=>$defen,'tongji'=>$statistics];
  128. return json($result);
  129. }
  130. public function edit($ids = null)
  131. {
  132. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  133. if (!$row) {
  134. $this->error(__('No Results were found'));
  135. }
  136. $rows = $this->model->get($ids);
  137. $row['seat_id'] = $rows->seat_id;
  138. $row['user_nickname'] = $rows->user_nickname;
  139. $row['user_username'] = $rows->user_username;
  140. $row['is_sure'] = $rows->is_sure;
  141. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  142. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  143. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  144. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  145. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  146. $diffInSeconds = $rows['endtime'] - $rows['starttime']; // 两个时间戳之间的差异(秒)
  147. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  148. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  149. $row['shijian'] = $minutes.'分'.$seconds.'秒';
  150. if (false === $this->request->isPost()) {
  151. $this->view->assign('other_jielun', $other_jielun);
  152. $this->view->assign('row', $row);
  153. return $this->view->fetch();
  154. }
  155. $params = $this->request->post('row/a');
  156. if (empty($params)) {
  157. $this->error(__('Parameter %s can not be empty', ''));
  158. }
  159. $params = $this->preExcludeFields($params);
  160. $result = false;
  161. Db::startTrans();
  162. try {
  163. //是否采用模型验证
  164. $koufen = 0;
  165. foreach ($other_jielun as $key => $value) {
  166. if(!empty($value['cx_score'])){
  167. $koufen = $koufen+abs($value['cx_score']);
  168. }
  169. }
  170. $params['total'] = 100-$params['fault_one_score']-$params['fault_two_score']-$params['fault_three_score']-$params['overtime_score']-$koufen;
  171. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  172. $rows->total_score = $params['total'];
  173. $rows->is_sure = 1;
  174. $rows->save();
  175. Db::commit();
  176. } catch (ValidateException|PDOException|Exception $e) {
  177. Db::rollback();
  178. $this->error($e->getMessage());
  179. }
  180. if (false === $result) {
  181. $this->error(__('No rows were updated'));
  182. }
  183. $this->success();
  184. }
  185. public function view($ids = null)
  186. {
  187. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  188. if (!$row) {
  189. $this->error(__('No Results were found'));
  190. }
  191. $rows = $this->model->get($ids);
  192. $row['fault_total'] = $rows->fault_total;
  193. $row['seat_id'] = $rows->seat_id;
  194. $row['user_nickname'] = $rows->user_nickname;
  195. $row['user_username'] = $rows->user_username;
  196. $row['user_depart_id'] = $rows->user_depart_id;
  197. $row['user_depart_name'] = Department::where('id',$rows->user_depart_id)->value('name');
  198. $row['start_time'] = $rows->start_time;
  199. $row['end_time'] = $rows->end_time;
  200. $fault_list = Db::name('real_exam_fault')->where(['exam_id'=>$ids,'flag'=>1])->select();
  201. $row['fault_name_one'] = Fault::where('fault_id',$fault_list[0]['fault_id'])->value('name');
  202. if($rows['fault_total']>0){
  203. if($rows['fault_total']==2){
  204. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  205. }
  206. if($rows['fault_total']==3){
  207. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  208. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  209. }
  210. }else{
  211. $row['fault_name_two'] = Fault::where('fault_id',$fault_list[1]['fault_id'])->value('name');
  212. $row['fault_name_three'] = Fault::where('fault_id',$fault_list[2]['fault_id'])->value('name');
  213. }
  214. $row['fault_score'] = 75-$row['fault_one_score']-$row['fault_two_score']-$row['fault_three_score'];
  215. $row['weixiu_score'] = 10-$row['overtime_score']??0;
  216. $other_jielun = !empty($row['other_jielun']) ? json_decode($row['other_jielun'],true):[];
  217. $koufen = 0;
  218. foreach ($other_jielun as $key => $value) {
  219. if(!empty($value['cx_score'])){
  220. $koufen = $koufen+abs($value['cx_score']);
  221. }
  222. }
  223. $report_score = 15-$koufen;
  224. $row['report_score'] = $report_score>0?$report_score:0;
  225. $total = $row['fault_score']+$row['report_score']+$row['weixiu_score'];
  226. Db::name('real_exam_score')->where('exam_id', $ids)->update(['total'=>$total]);
  227. Db::name('real_exam')->where('exam_id', $ids)->update(['total_score'=>$total]);
  228. $row['total'] = $total;
  229. $this->view->assign('other_jielun', $other_jielun);
  230. $diffInSeconds = $rows['endtime'] - $rows['starttime']; // 两个时间戳之间的差异(秒)
  231. $minutes = floor($diffInSeconds / 60); // 计算分钟数
  232. $seconds = $diffInSeconds % 60; // 计算剩余的秒数
  233. $row['shijian'] = $minutes.'分'.$seconds.'秒';
  234. $this->view->assign('row', $row);
  235. return $this->view->fetch();
  236. }
  237. //正在考试
  238. public function persent($ids = null)
  239. {
  240. //设置过滤方法
  241. $this->request->filter(['strip_tags', 'trim']);
  242. if (false === $this->request->isAjax()) {
  243. $this->assignConfig('ids', $ids);
  244. return $this->view->fetch();
  245. }
  246. //如果发送的来源是 Selectpage,则转发到 Selectpage
  247. if ($this->request->request('keyField')) {
  248. return $this->selectpage();
  249. }
  250. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  251. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  252. $list = Db::name('real_exam_comp_request')
  253. // ->where('exam_collection_type',3)
  254. ->where($where)->where('exam_id','in',$exam_ids)
  255. ->order($sort, $order)
  256. ->paginate($limit);
  257. unset($v);
  258. $result = ['total' => $list->total(), 'rows' => $list->items()];
  259. return json($result);
  260. }
  261. public function examing($ids = null)
  262. {
  263. //设置过滤方法
  264. $this->request->filter(['strip_tags', 'trim']);
  265. if (false === $this->request->isAjax()) {
  266. $this->assignConfig('ids', $ids);
  267. return $this->view->fetch();
  268. }
  269. //如果发送的来源是 Selectpage,则转发到 Selectpage
  270. if ($this->request->request('keyField')) {
  271. return $this->selectpage();
  272. }
  273. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  274. $list = Db::name('seat')->select();
  275. foreach ($list as $key => $value) {
  276. $exam = $this->model->where('exam_collection_id',$ids)->order('exam_id desc')->where('seat_id',$value['seat_num'])->find();
  277. $user_username = '';
  278. $user_nickname = '';
  279. $exam_status = '未登录';
  280. $total_score = 0;
  281. $fault_names = '';
  282. $exam_id = 0;
  283. $simtype = '';
  284. $sim_state = '';
  285. if(!empty($exam)){
  286. if(!empty($value['current_sim_id'])){
  287. $sim = Db::name('sim')->where('sim_id',$value['current_sim_id'])->find();
  288. if(!empty($sim['sim_state'])){
  289. switch ($sim['sim_state']) {
  290. case '1':
  291. $sim_state = '在线';
  292. break;
  293. case '2':
  294. $sim_state = '离线';
  295. break;
  296. }
  297. }
  298. $simtype = $sim['sim_type'];
  299. }
  300. $user_username = $exam['user_username']??'';
  301. $user_nickname = $exam['user_nickname']??'';
  302. if($exam['exam_status']==1){
  303. $exam_status= '已登录未开始考试';
  304. }else if($exam['exam_status']==4){
  305. $exam_status= '已开始考试';
  306. }else if($exam['exam_status']==5){
  307. $exam_status= '已交卷';
  308. }
  309. $total_score = $exam['total_score']??'';
  310. $fault_names = $exam['fault_names']??'';
  311. $exam_id = $exam['exam_id']??0;
  312. $simtype = $simtype;
  313. }
  314. $list[$key]['user_username'] = $user_username;
  315. $list[$key]['user_nickname'] = $user_nickname;
  316. $list[$key]['exam_status'] = $exam_status;
  317. $list[$key]['total_score'] = $total_score;
  318. $list[$key]['fault_names'] = $fault_names;
  319. $list[$key]['exam_id'] = $exam_id;
  320. $list[$key]['sim_state'] = $sim_state;
  321. $list[$key]['sim_type'] = $simtype;
  322. }
  323. $result = ['total' => count($list), 'rows' => $list];
  324. return json($result);
  325. }
  326. public function score($ids = null)
  327. {
  328. //设置过滤方法
  329. $this->request->filter(['strip_tags', 'trim']);
  330. if (false === $this->request->isAjax()) {
  331. $this->assignConfig('ids', $ids);
  332. return $this->view->fetch();
  333. }
  334. //如果发送的来源是 Selectpage,则转发到 Selectpage
  335. if ($this->request->request('keyField')) {
  336. return $this->selectpage();
  337. }
  338. $exam_ids = $this->model->where('exam_collection_id',$ids)->column('exam_id');
  339. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  340. $list = ExamsScore::where($where)->where('exam_id','in',$exam_ids)
  341. ->order($sort, $order)
  342. ->paginate($limit);
  343. foreach ($list as $k => $v){
  344. $exam = $this->model->where(['exam_id'=>$v['exam_id']])->find();
  345. if($exam){
  346. $v->seat_id = $exam['seat_id'];
  347. $v->user_username = $exam['user_username'];
  348. $v->user_nickname = $exam['user_nickname'];
  349. $v->sim_type = $exam['sim_type'];
  350. }
  351. }
  352. unset($v);
  353. $result = ['total' => $list->total(), 'rows' => $list->items()];
  354. return json($result);
  355. }
  356. public function handle($type = null,$ids = null)
  357. {
  358. $row = Db::name('real_exam_comp_request')->where('rel_id',$ids)->find($ids);
  359. if(!$row){
  360. $this->error('未找到记录');
  361. }
  362. if($type ==1){
  363. $request_status = 2;
  364. }
  365. if($type==2)
  366. {
  367. $request_status= 3;
  368. }
  369. Db::name('real_exam_comp_request')->where('rel_id',$ids)->update(['request_status'=>$request_status]);
  370. //同步更新考试表json串
  371. $exam_info = $this->model->get($row['exam_id']);
  372. if(!empty($exam_info)){
  373. $other_replace = json_decode($exam_info['other_replace'],true)??[];
  374. foreach ($other_replace as $k=>$v){
  375. if($v['fault_id'] == $row['fault_id']){
  376. $other_replace[$k]['request_status'] = $request_status;
  377. }
  378. }
  379. $exam_info->other_replace = json_encode($other_replace);
  380. $exam_info->save();
  381. unset($v);
  382. }
  383. $this->success();
  384. }
  385. //啦
  386. public function editscore($ids = "")
  387. {
  388. $row = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  389. if (!$row) {
  390. $this->error(__('No Results were found'));
  391. }
  392. $rows = $this->model->get($ids);
  393. $params = $this->request->post('row/a');
  394. if (empty($params)) {
  395. $this->error(__('Parameter %s can not be empty', ''));
  396. }
  397. $params = $this->preExcludeFields($params);
  398. $result = false;
  399. Db::startTrans();
  400. try {
  401. //是否采用模型验证
  402. $result = Db::name('real_exam_score')->where('id', $row['id'])->update($params);
  403. $info = Db::name('real_exam_score')->where('exam_id', $ids)->find();
  404. $total =100-$info['fault_one_score']-$info['fault_two_score']-$info['fault_three_score']-$info['xianxian_score']-$info['yuanyin_socre']-$info['buwei_score']-$info['fangfa_score']-$info['overtime_score']-$info['jielun_score'];;
  405. $rows->total_score = $total;
  406. $rows->is_sure = 1;
  407. $rows->save();
  408. $result = Db::name('real_exam_score')->where('id', $row['id'])->update(['total'=>$total]);
  409. Db::commit();
  410. } catch (ValidateException|PDOException|Exception $e) {
  411. Db::rollback();
  412. $this->error($e->getMessage());
  413. }
  414. if (false === $result) {
  415. $this->error(__('No rows were updated'));
  416. }
  417. $this->success();
  418. }
  419. public function export()
  420. {
  421. set_time_limit(0);
  422. ini_set('memory_limit', '2560M');
  423. //如果发送的来源是 Selectpage,则转发到 Selectpage
  424. if ($this->request->request('keyField')) {
  425. return $this->selectpage();
  426. }
  427. $filter = $this->request->get("filter", '');
  428. $filter = (array)json_decode($filter, true);
  429. $op = $this->request->get("op", '');
  430. $op = (array)json_decode($op, true);
  431. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  432. $list = $this->model
  433. ->where($where)->where($this->whereExtend)
  434. ->order($sort, $order)
  435. ->paginate($limit);
  436. $xlsName = '学员成绩信息';
  437. $this->exportExcel($xlsName,'Excel2007', $list);
  438. }
  439. /**
  440. * 输出到浏览器(需要设置header头)
  441. * @param string $fileName 文件名
  442. * @param string $fileType 文件类型
  443. */
  444. function exportExcel($fileName, $fileType,$data)
  445. {
  446. //文件名称校验
  447. if (!$fileName) {
  448. trigger_error('文件名不能为空', E_USER_ERROR);
  449. }
  450. //Excel文件类型校验
  451. $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];
  452. if (!in_array($fileType, $type)) {
  453. trigger_error('未知文件类型', E_USER_ERROR);
  454. }
  455. $ext = '';
  456. if ($fileType == 'Excel2007' || $fileType == 'Xlsx') {
  457. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  458. header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"');
  459. header('Cache-Control: max-age=0');
  460. $ext = 'Xlsx';
  461. } else { //Excel5
  462. header('Content-Type: application/vnd.ms-excel');
  463. header('Content-Disposition: attachment;filename="' . $fileName . '.xls"');
  464. header('Cache-Control: max-age=0');
  465. $ext = 'Xls';
  466. }
  467. $spreadsheet = new Spreadsheet();
  468. $worksheet = $spreadsheet->getActiveSheet();
  469. $worksheet->getColumnDimension('A')->setWidth(15);
  470. $worksheet->getColumnDimension('B')->setWidth(15);
  471. $worksheet->getColumnDimension('C')->setWidth(15);
  472. $worksheet->getColumnDimension('D')->setWidth(15);
  473. $worksheet->getRowDimension('1')->setRowHeight(25);
  474. //设置工作表标题名称
  475. $worksheet->setTitle('学员成绩信息');
  476. $worksheet->setCellValue('A1',"序号");
  477. $worksheet->setCellValue('B1',"学号");
  478. $worksheet->setCellValue('C1',"姓名");
  479. $worksheet->setCellValue('D1',"总分");
  480. foreach ($data as $ky => $value)
  481. {
  482. $lie = $ky+2;
  483. $worksheet->setCellValue('A'.$lie,++$ky);
  484. $worksheet->setCellValue('B'.$lie,$value['user_username']);
  485. $worksheet->setCellValue('C'.$lie,$value['user_nickname']);
  486. $worksheet->setCellValue('D'.$lie,$value['total_score']);
  487. }
  488. $titlestyleArray = [
  489. 'font' => [
  490. 'name' => '黑体',
  491. 'size' => 14
  492. ],
  493. ];
  494. $headerStyleArray = [
  495. 'font' => [
  496. 'name' => '方正小标宋简体',
  497. 'size' => 18
  498. ],
  499. ];
  500. $commonStyleArray = [
  501. 'alignment' => [
  502. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  503. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  504. 'wrapText' => true,
  505. ]
  506. ];
  507. $borderStyleArray = [
  508. 'borders' => [
  509. 'allBorders' => [
  510. // 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  511. ],
  512. ],
  513. 'font' => [
  514. 'name' => '黑体',
  515. ],
  516. 'alignment' => [
  517. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  518. 'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER,
  519. 'wrapText' => true,
  520. ]
  521. ];
  522. $worksheet->getStyle('A1:D1')->applyFromArray($titlestyleArray);
  523. $worksheet->getStyle('A1:D1')->applyFromArray($commonStyleArray);
  524. foreach ($data as $k=>$v){
  525. $kk = $k+2;
  526. $worksheet->getStyle('A'.$kk.':D'.$kk)->applyFromArray($borderStyleArray);
  527. }
  528. // $worksheet->getStyle('A3:R3')->applyFromArray($commonStyleArray);
  529. // $worksheet->getStyle('A'.$footer_total.':R'.$footer_total)->applyFromArray($commonStyleArray);
  530. // $worksheet->getStyle('A1:C1')->applyFromArray($borderStyleArray);
  531. // $worksheet->mergeCells('A1:R1');
  532. $writer = IOFactory::createWriter($spreadsheet,$ext);
  533. $writer->save('php://output');
  534. die();
  535. }
  536. }