PhpOffice.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: yckj-yf7
  5. * Date: 2021/9/26
  6. * Time: 11:14
  7. */
  8. namespace app\common\util;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  11. class PhpOffice
  12. {
  13. public static function export()
  14. {
  15. #获取对象
  16. $spreadsheet = new Spreadsheet();
  17. #获取工作簿
  18. $sheet = $spreadsheet->getActiveSheet();
  19. $QuestionAnswerRecordModel = new \app\common\model\Order();
  20. $pageList = $QuestionAnswerRecordModel->analysis();
  21. $list = $pageList['data'];
  22. $arr = [];
  23. #问题
  24. $title = '问卷提交统计Excel';
  25. $sheet->setCellValue(2,1,$title);
  26. $sheet->getStyle('B1')
  27. ->getFont()->setBold(true)
  28. ->setName('宋体')
  29. ->setSize(20);
  30. $styleArray = [
  31. 'borders' => [
  32. 'left' => [
  33. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN //细边框
  34. ],
  35. 'right' => [
  36. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN //细边框
  37. ],
  38. 'top' => [
  39. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN //细边框
  40. ],
  41. 'bottom' => [
  42. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN //细边框
  43. ],
  44. ],
  45. 'alignment' => [
  46. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  47. ],
  48. ];
  49. $styleArrayTitle = [
  50. 'alignment' => [
  51. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  52. ],
  53. ];
  54. $sheet->getColumnDimension('B')->setWidth(70);
  55. $sheet->getStyle('B1')->applyFromArray($styleArrayTitle);
  56. $i = 2;
  57. foreach ($list['PaperQuestion'] as $key => $value) {
  58. // $sheet->setCellValueByColumnAndRow(2,$i+1,'选项');
  59. // $sheet->setCellValueByColumnAndRow(8,$i+1,'小计');
  60. // $sheet->setCellValueByColumnAndRow(9,$i+1,'比例');
  61. // $sheet->mergeCells('')
  62. $sheet->setCellValue(2,$i+1,$value['content']);
  63. $sheet->getStyle('B'.($i+1).':'.'F'.($i+1))->applyFromArray($styleArray);
  64. $sheet->getStyle('B'.($i+1))->getFont()->setBold(true)->setName('宋体')->setSize(15);
  65. foreach ($value['paperQuestionAnswer'] as $key2=>$value2){
  66. $sheet->setCellValue(2,$i+2,$value2['content']);
  67. $sheet->setCellValue(5,$i+2,$value2['num']);
  68. // $sheet->setCellValueByColumnAndRow(6,$i+2,$value2['avg'].'%');
  69. $sheet->getStyle('B'.($i+2).':'.'F'.($i+2))->applyFromArray($styleArray);
  70. $i++;
  71. }
  72. $i+=3;
  73. }
  74. self::exportBinary($spreadsheet);
  75. // $this->exportFile($spreadsheet);
  76. }
  77. /**
  78. * ##导出一 直接生成xlsx文件
  79. */
  80. public static function exportFile($spreadsheet)
  81. {
  82. $writer = new Xlsx($spreadsheet);
  83. $root = $_SERVER['DOCUMENT_ROOT'];
  84. $filePath = $root . '/excel/';
  85. if (!is_dir($filePath)) {
  86. mkdir($filePath, 0777, true);
  87. }
  88. ##文件地址
  89. $file = time() . '.xlsx';
  90. $fileName = $filePath . $file;
  91. $writer->save($fileName);
  92. $data = [
  93. 'fileName'=>$fileName,
  94. 'url'=>request()->host().'/excel/'.$file,
  95. ];
  96. return $data;
  97. }
  98. /**
  99. * 导出二 直接返回二进制数据
  100. */
  101. public static function exportBinary($spreadsheet)
  102. {
  103. header('Content-Type:application/vnd.ms-excel');
  104. header('Content-Disposition:attachment;filename=' . time() . '.xls');
  105. header('Cache-Control:max-age=0');
  106. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
  107. $writer->save('php://output');
  108. exit;
  109. }
  110. }