PhpSpreadsheetExportV2.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\common\util;
  3. use PhpOffice\PhpSpreadsheet\Exception;
  4. use PhpOffice\PhpSpreadsheet\IOFactory;
  5. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  6. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  7. use PhpOffice\PhpSpreadsheet\Style\Border;
  8. use PhpOffice\PhpSpreadsheet\Style\Color;
  9. use PhpOffice\PhpSpreadsheet\Style\Fill;
  10. use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
  11. use think\route\Domain;
  12. class PhpSpreadsheetExportV2
  13. {
  14. public static array $excelCol = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  15. 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
  16. public static bool $setBold = false; //是否加粗
  17. public static string $setName = '宋体'; //字体
  18. public static string $setSize = '12'; //字体大小
  19. public static string $setBgRGB = 'FFFFFFFF'; //单元格背景色
  20. public static string $setFontRGB = 'FF000000'; //字体颜色
  21. public static array $styleArray = [
  22. 'alignment' => [
  23. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  24. 'vertical' => Alignment::VERTICAL_CENTER
  25. ],
  26. 'borders' => [
  27. 'allBorders' => [
  28. 'borderStyle' => Border::BORDER_THIN,
  29. 'color' => ['argb' => '000000'],
  30. ],
  31. ],
  32. ];
  33. /**
  34. * 保存文件
  35. * @param object $spreadsheet
  36. * @param $fileName
  37. * @param string $suffix
  38. */
  39. public static function saveFile(object $spreadsheet, $fileName, string $suffix = 'xlsx')
  40. {
  41. $writer = IOFactory::createWriter($spreadsheet, ucwords($suffix));
  42. $writer->save($fileName, true);
  43. }
  44. /**
  45. * 导出excel文件
  46. * @param \ArrayAccess $data
  47. * @param array<array> $tableHeader
  48. * @param string $fileName
  49. * @param array $mergeCells
  50. * @return array
  51. */
  52. public static function outputFile($data, $tableHeader, $fileName, $mergeCells = [])
  53. {
  54. $path = "/storage/topic/output_excel/" . date('Ymd') . "/";
  55. $dir = app()->getRootPath() . "public" . $path;
  56. if (!file_exists($dir)) {
  57. mkdir($dir, '0755', true);
  58. }
  59. $fileName = $fileName . ".xlsx";
  60. $file = $dir . $fileName;
  61. $SpreadSheet = new Spreadsheet();
  62. $sheet = $SpreadSheet->getActiveSheet();
  63. \app\common\util\PhpSpreadsheetExportV2::write($sheet, $data, $tableHeader, $mergeCells);
  64. \app\common\util\PhpSpreadsheetExportV2::saveFile($SpreadSheet, $file);
  65. $url = self::get_domain() . getVirRootDir() . $path . $fileName;
  66. return ['url' => $url, 'file' => $file];
  67. }
  68. /**
  69. * 写入数据
  70. * @param $sheet
  71. * @param $data
  72. * @param $tableHeader
  73. * @param $mergeCells
  74. * @return bool
  75. */
  76. public static function write($sheet, $data, $tableHeader, $mergeCells): bool
  77. {
  78. $totalCol = 0;
  79. //设置表头合并单元格
  80. foreach ($mergeCells as $row => $rows) {
  81. $i = 0;
  82. foreach ($rows as $col => $colValue) {
  83. //合并单元格
  84. $sheet->mergeCells($col);
  85. //设置样式
  86. self::setStyle($sheet, $i, $totalCol, $row);
  87. //单元格内容写入
  88. $sheet->setCellValue(substr($col, 0, strpos($col, ":")), $colValue);
  89. $i++;
  90. }
  91. }
  92. $totalCol = count($mergeCells);
  93. //设置表头
  94. foreach ($tableHeader as $row => $rows) {
  95. $headerRowDatas = array_values($rows);
  96. foreach ($headerRowDatas as $col => $colValue) {
  97. //设置样式
  98. self::setStyle($sheet, $col, $totalCol, $row);
  99. //单元格内容写入
  100. $sheet->getCell(self::$excelCol[$col] . ($totalCol + $row + 1))->setValueExplicit($colValue,'s');//统一格式化为字符串
  101. // $sheet->setCellValue(self::$excelCol[$col] . ($totalCol + $row + 1), $colValue);
  102. }
  103. }
  104. $totalCol += count($tableHeader);
  105. //设置内容
  106. foreach ($data as $row => $rows) {
  107. $rowDatas = array_values($rows);
  108. foreach ($rowDatas as $col => $colValue) {
  109. // 单元格内容写入
  110. // $sheet->setCellValue(self::$excelCol[$col] . ($totalCol + $row + 1), $colValue);
  111. $sheet->getCell(self::$excelCol[$col] . ($totalCol + $row + 1))->setValueExplicit($colValue,'s');//统一格式化为字符串
  112. }
  113. }
  114. return true;
  115. }
  116. /**
  117. * 设置单元格样式
  118. * @param $sheet //某个sheet
  119. * @param $col //某列
  120. * @param $totalCol //总行数
  121. * @param $row //某行
  122. */
  123. public static function setStyle($sheet, $col, $totalCol, $row): void
  124. {
  125. //设置单元格居中
  126. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))->applyFromArray(self::$styleArray);
  127. //设置单元格
  128. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
  129. ->getFill()
  130. ->setFillType(Fill::FILL_SOLID)
  131. ->getStartColor()
  132. ->setRGB(self::$setBgRGB);
  133. //设置单元格字体样式、字体、字体大小
  134. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
  135. ->getFont()
  136. ->setBold(self::$setBold)
  137. ->setName(self::$setName)
  138. ->setSize(self::$setSize);
  139. //设置字体颜色
  140. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
  141. ->getFont()
  142. ->getColor()->setRGB(self::$setFontRGB);
  143. }
  144. /**
  145. * 获取当前网站的域名地址
  146. *
  147. * @return string 域名地址
  148. */
  149. protected static function get_domain()
  150. {
  151. $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
  152. return $sys_protocal . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
  153. }
  154. }