ExcelWriter.php 3.0 KB

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