ExcelWriter.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace excel;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  5. class ExcelWriter
  6. {
  7. private $spreadsheet;
  8. private $sheet;
  9. private $fileUrl;
  10. public string $savePath = '';
  11. public function __construct()
  12. {
  13. $this->spreadsheet = new Spreadsheet();
  14. $this->sheet = $this->spreadsheet->getActiveSheet();
  15. }
  16. public function setHeaders(array $headers, $row = 1)
  17. {
  18. foreach ($headers as $column => $header) {
  19. $this->sheet->setCellValue($this->getColumnLetter($column + 1) . $row, $header);
  20. }
  21. }
  22. public function addData(array $data, $startRow = 2)
  23. {
  24. foreach ($data as $rowIndex => $row) {
  25. foreach ($row as $column => $value) {
  26. $this->sheet->setCellValue($this->getColumnLetter($column + 1) . ($startRow + $rowIndex), $value);
  27. if (is_numeric($value) && strlen($value) > 10) {
  28. $this->sheet->getCell($this->getColumnLetter($column + 1) . ($startRow + $rowIndex))->setValueExplicit($value);
  29. }
  30. $this->sheet->getColumnDimension($this->getColumnLetter($column + 1))->setWidth(20);
  31. }
  32. }
  33. }
  34. public function save($filename)
  35. {
  36. $savePath = $this->savePath?:"./exports/";
  37. $filePath = $savePath . $filename;
  38. $writer = new Xlsx($this->spreadsheet);
  39. $writer->save($filePath);
  40. $this->fileUrl = "/exports/". $filename;
  41. }
  42. public function fileUrl()
  43. {
  44. return $this->fileUrl;
  45. }
  46. public function getCellValue($row,$value)
  47. {
  48. $values = explode('.',$value);
  49. $count = count($values);
  50. switch ($count){
  51. case 1:
  52. return $row[$values[0]]??'';
  53. case 2:
  54. return $row[$values[0]][$values[1]]??'';
  55. case 3:
  56. return $row[$values[0]][$values[1]][$values[2]]??'';
  57. default:
  58. return '';
  59. }
  60. }
  61. /**
  62. * 生成并保存 Excel 文件
  63. * @return void
  64. */
  65. function generateExcelFile(array $headers, array $lists,string $filename, array $map = [],string $suffix = 'xlsx'): void
  66. {
  67. $filename = ($filename?:'').('-'.date('YmdHis'));
  68. //if(!in_array($suffix,['xlsx','xls'])) throw new \Exception('文件格式不正确');
  69. $this->setHeaders($headers);
  70. $data = array_map(function($row) use ($map) {
  71. if(empty($map)){
  72. return $row;
  73. }
  74. $tmp_arr = [];
  75. foreach ($map as $value){
  76. if (is_callable($value)){
  77. $tmp_arr[] = $value($row);
  78. }elseif (is_string($value)){
  79. $tmp_arr[] = $this->getCellValue($row,$value);
  80. }
  81. }
  82. return $tmp_arr;
  83. }, $lists);
  84. $this->addData($data);
  85. $this->save($filename.".".$suffix);
  86. }
  87. private function getColumnLetter($columnIndex)
  88. {
  89. return \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($columnIndex);
  90. }
  91. }