PhpSpreadsheetExport.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. class PhpSpreadsheetExport
  11. {
  12. public function index()
  13. {
  14. $data = [
  15. ['title1' => '111', 'title2' => '111', 'title3' => 666],
  16. ['title1' => '222', 'title2' => '222'],
  17. ['title1' => '333', 'title2' => '333']
  18. ];
  19. $tableHeader = [
  20. ['第一行标题', '第一行标题'],
  21. ['第二行标题', '第二行标题']
  22. ];
  23. $mergeCells = [
  24. ['A1:B1' => '第一行标题', 'C1:F1' => '第一111行标题'],
  25. ['A2:B2' => '第一行标题', 'C2:E2' => '第一222行标题'],
  26. ];
  27. $fileName = "8888.xlsx";
  28. $this->saveFile($data, $fileName, $tableHeader);
  29. }
  30. 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',
  31. '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'];
  32. public static bool $setBold = false; //是否加粗
  33. public static string $setName = '宋体'; //字体
  34. public static string $setSize = '12'; //字体大小
  35. public static string $setBgRGB = 'FFFF00'; //单元格背景色
  36. public static string $setFontRGB = 'FF000000'; //字体颜色
  37. public static array $styleArray = [
  38. 'alignment' => [
  39. 'horizontal' => Alignment::HORIZONTAL_CENTER,
  40. 'vertical' => Alignment::VERTICAL_CENTER
  41. ],
  42. 'borders' => [
  43. 'allBorders' => [
  44. 'borderStyle' => Border::BORDER_THIN,
  45. 'color' => ['argb' => '000000'],
  46. ],
  47. ],
  48. ];
  49. // /**
  50. // * 读取excel
  51. // * @param $filePath
  52. // * @param int $pageIndex
  53. // * @param int $readRow
  54. // * @return array
  55. // * @throws Exception
  56. // */
  57. // public static function read($filePath, int $pageIndex = 0, int $readRow = 0): array
  58. // {
  59. // //加载文件
  60. // $spreadSheet = IOFactory::load($filePath);
  61. // //获取文件内容
  62. // $workSheet = $spreadSheet->getSheet($pageIndex)->toArray('', true, true, false);
  63. // //删除表头几行
  64. // if ($readRow > 0) {
  65. // for ($i = 0; $i < $readRow; $i++) {
  66. // array_shift($workSheet);
  67. // }
  68. // }
  69. // return $workSheet;
  70. // }
  71. //
  72. // /**
  73. // * @param $data
  74. // * @param $fileName
  75. // * @param array $tableHeader
  76. // * @param array $mergeCells
  77. // * @param string $suffix
  78. // * @return void
  79. // * @throws Exception
  80. // * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  81. // */
  82. // public static function download($data, $fileName, array $tableHeader = [], array $mergeCells = [], string $suffix = 'xlsx'): void
  83. // {
  84. // $spreadsheet = self::write($data, $tableHeader, $mergeCells);
  85. // // 将输出重定向到客户端的网络浏览器(Xlsx)
  86. // header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  87. // header('Content-Disposition: attachment;filename="' . $fileName . '"');//文件名
  88. // header('Cache-Control: max-age=0');
  89. // // 如果你服务于IE 9,那么以下可能是需要的
  90. // header('Cache-Control: max-age=1');
  91. // // 如果您通过SSL为工业工程服务,那么可能需要以下内容
  92. // header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  93. // header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  94. // header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  95. // header('Pragma: public'); // HTTP/1.0
  96. // $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, ucwords($suffix));
  97. // $writer->save('php://output');
  98. // }
  99. /**
  100. * 保存文件
  101. * @param $data
  102. * @param $fileName
  103. * @param array $tableHeader
  104. * @param array $mergeCells
  105. * @param string $suffix
  106. * @return bool
  107. */
  108. public function saveFile($data, $fileName, array $tableHeader = [], array $mergeCells = [], string $suffix = 'xlsx'): bool
  109. {
  110. try {
  111. $spreadsheet = self::write($data, $tableHeader, $mergeCells);
  112. $writer = IOFactory::createWriter($spreadsheet, ucwords($suffix));
  113. $writer->save($fileName, true);
  114. return true;
  115. } catch (\Exception) {
  116. return false;
  117. }
  118. }
  119. /**
  120. * 写入数据
  121. * @param $data
  122. * @param $tableHeader
  123. * @param $mergeCells
  124. * @return Spreadsheet
  125. * @throws Exception
  126. */
  127. public static function write($data, $tableHeader, $mergeCells): Spreadsheet
  128. {
  129. // 创建excel对象
  130. $spreadsheet = new Spreadsheet();
  131. $sheet = $spreadsheet->getActiveSheet();
  132. $totalCol = 0;
  133. //设置表头合并单元格
  134. foreach ($mergeCells as $row => $rows) {
  135. $i = 0;
  136. foreach ($rows as $col => $colValue) {
  137. //合并单元格
  138. $sheet->mergeCells($col);
  139. //设置样式
  140. self::setStyle($sheet, $i, $totalCol, $row);
  141. //单元格内容写入
  142. $sheet->setCellValue(substr($col, 0, strpos($col, ":")), $colValue);
  143. $i++;
  144. }
  145. }
  146. $totalCol = count($mergeCells);
  147. //设置表头
  148. foreach ($tableHeader as $row => $rows) {
  149. $headerRowDatas = array_values($rows);
  150. foreach ($headerRowDatas as $col => $colValue) {
  151. //设置样式
  152. self::setStyle($sheet, $col, $totalCol, $row);
  153. //单元格内容写入
  154. $sheet->setCellValue(self::$excelCol[$col] . ($totalCol + $row + 1), $colValue);
  155. }
  156. }
  157. $totalCol += count($tableHeader);
  158. //设置内容
  159. foreach ($data as $row => $rows) {
  160. $rowDatas = array_values($rows);
  161. foreach ($rowDatas as $col => $colValue) {
  162. // 单元格内容写入
  163. $sheet->setCellValue(self::$excelCol[$col] . ($totalCol + $row + 1), $colValue);
  164. }
  165. }
  166. return $spreadsheet;
  167. }
  168. /**
  169. * 设置单元格样式
  170. * @param $sheet //某个sheet
  171. * @param $col //某列
  172. * @param $totalCol //总行数
  173. * @param $row //某行
  174. */
  175. public static function setStyle($sheet, $col, $totalCol, $row): void
  176. {
  177. //设置单元格居中
  178. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))->applyFromArray(self::$styleArray);
  179. //设置单元格
  180. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
  181. ->getFill()
  182. ->setFillType(Fill::FILL_SOLID)
  183. ->getStartColor()
  184. ->setRGB(self::$setBgRGB);
  185. //设置单元格字体样式、字体、字体大小
  186. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
  187. ->getFont()
  188. ->setBold(self::$setBold)
  189. ->setName(self::$setName)
  190. ->setSize(self::$setSize);
  191. //设置字体颜色
  192. $sheet->getStyle(self::$excelCol[$col] . ($totalCol + $row + 1))
  193. ->getFont()
  194. ->getColor()->setRGB(self::$setFontRGB);
  195. }
  196. }