PhpSpreadsheetService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace service;
  12. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  13. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  14. use service\JsonService as Json;
  15. use think\Request;
  16. class PhpSpreadsheetService
  17. {
  18. //PHPExcel实例化对象
  19. private static $PHPExcel = null;
  20. //表头计数
  21. protected static $count;
  22. //表头占行数
  23. protected static $topNumber = 3;
  24. //表能占据表行的字母对应self::$cellkey
  25. protected static $cells;
  26. //表头数据
  27. protected static $data = [];
  28. //文件名
  29. protected static $title = '订单导出';
  30. //行宽
  31. protected static $where = 30;
  32. //行高
  33. protected static $height = 50;
  34. //表行名
  35. private static $cellKey = array(
  36. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  37. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  38. 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM',
  39. 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'
  40. );
  41. //设置style
  42. private static $styleArray = array(
  43. 'borders' => array(
  44. 'allborders' => array(
  45. 'style' => \PHPExcel_Style_Border::BORDER_THIN,//细边框
  46. ),
  47. ),
  48. 'font' => [
  49. 'bold' => true
  50. ],
  51. 'alignment' => [
  52. 'horizontal' => \PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  53. 'vertical' => \PHPExcel_Style_Alignment::VERTICAL_CENTER
  54. ]
  55. );
  56. /**
  57. *设置字体格式
  58. * @param $title string 必选
  59. * return string
  60. */
  61. public static function setUtf8($title)
  62. {
  63. return iconv('utf-8', 'gb2312', $title);
  64. }
  65. /**
  66. * 通用导出方法。传入参数即可
  67. * @param unknown $filename 导出的excel文件名称,不包括后缀
  68. * @param unknown $rows 要导出的数据,数组
  69. * @param unknown $head 要导出数据的表头,数组
  70. * @param unknown $keys 要导出数据的键值对对应
  71. */
  72. public static function outdata($filename, $rows = [], $head = [])
  73. {
  74. $count = count($head); //计算表头数量
  75. $spreadsheet = new Spreadsheet();
  76. $sheet = $spreadsheet->getActiveSheet();
  77. //设置样式,设置剧中,加边框,设置行高
  78. $styleArray = [
  79. 'alignment' => [
  80. 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
  81. ],
  82. 'borders' => [
  83. 'allBorders' => [
  84. 'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
  85. 'color' => ['argb' => '6184542'],
  86. ],
  87. ],
  88. ];
  89. $rows_count = count($rows);
  90. $sheet->getDefaultRowDimension()->setRowHeight(18);//设置默认行高。
  91. $sheet->getStyle('A1:' . strtoupper(chr($count + 65 - 1)) . strval($rows_count + 1))->applyFromArray($styleArray);
  92. $sheet->getStyle('A4:' . strtoupper(chr($count + 65 - 1)) . '1')->getFont()->setBold(true)->setName('Arial')->setSize(10)->applyFromArray($styleArray);
  93. //设置样式结束
  94. //写入表头信息
  95. for ($i = 65; $i < $count + 65; $i++) {
  96. //数字转字母从65开始,循环设置表头:
  97. $sheet->setCellValue(strtoupper(chr($i)) . '1', $head[$i - 65]);
  98. }
  99. //写入数据信息
  100. foreach ($rows as $key => $item) {
  101. //循环设置单元格:
  102. //$key+2,因为第一行是表头,所以写到表格时 从第二行开始写
  103. for ($i = 65; $i < $count + 65; $i++) {
  104. //数字转字母从65开始:
  105. $sheet->setCellValue(strtoupper(chr($i)) . ($key + 2), $item[$i - 65]);
  106. $spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setWidth(30); //固定列宽
  107. // 支持换行
  108. // $sheet->getStyle(strtoupper(chr($i)))->getAlignment()->setWrapText(true);
  109. }
  110. }
  111. if(ob_get_length()) ob_end_clean();
  112. //header('Content-Type: application/vnd.ms-excel');xls
  113. header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//xlsx
  114. header('Content-Disposition: attachment;filename="' . $filename . '"');
  115. header('Cache-Control: max-age=0');
  116. $writer = new Xlsx($spreadsheet);
  117. $writer->save('php://output');
  118. //删除清空:
  119. $spreadsheet->disconnectWorksheets();
  120. unset($spreadsheet);
  121. exit;
  122. }
  123. }