BillExport.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Module;
  3. use Excel;
  4. use Maatwebsite\Excel\Concerns\FromCollection;
  5. use Maatwebsite\Excel\Concerns\WithEvents;
  6. use Maatwebsite\Excel\Concerns\WithHeadings;
  7. use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
  8. use Maatwebsite\Excel\Concerns\WithTitle;
  9. use Maatwebsite\Excel\Events\AfterSheet;
  10. use PhpOffice\PhpSpreadsheet\Cell\DataValidation;
  11. use PhpOffice\PhpSpreadsheet\Writer\Exception;
  12. class BillExport implements WithHeadings, WithEvents, FromCollection, WithTitle, WithStrictNullComparison
  13. {
  14. public $title;
  15. public $headings = [];
  16. public $data = [];
  17. public $typeLists = [];
  18. public $typeNumber = 0;
  19. public function __construct($title, array $data)
  20. {
  21. $this->title = $title;
  22. $this->data = $data;
  23. }
  24. public static function create($data = [], $title = "Sheet1") {
  25. if (is_string($data)) {
  26. list($title, $data) = [$data, $title];
  27. }
  28. if (!is_array($data)) {
  29. $data = [];
  30. }
  31. return new BillExport($title, $data);
  32. }
  33. public function setTitle($title) {
  34. $this->title = $title;
  35. return $this;
  36. }
  37. public function setHeadings(array $headings) {
  38. $this->headings = $headings;
  39. return $this;
  40. }
  41. public function setData(array $data) {
  42. $this->data = $data;
  43. return $this;
  44. }
  45. public function setTypeList(array $typeList, $number = 0) {
  46. $this->typeLists = $typeList;
  47. $this->typeNumber = $number;
  48. return $this;
  49. }
  50. public function store($fileName = '') {
  51. if (empty($fileName)) {
  52. $fileName = date("YmdHis") . '.xls';
  53. }
  54. try {
  55. return Excel::store($this, $fileName);
  56. } catch (Exception $e) {
  57. return "导出错误:" . $e->getMessage();
  58. } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
  59. return "导出错误:" . $e->getMessage();
  60. }
  61. }
  62. public function download($fileName = '') {
  63. if (empty($fileName)) {
  64. $fileName = date("YmdHis") . '.xls';
  65. }
  66. try {
  67. return Excel::download($this, $fileName);
  68. } catch (Exception $e) {
  69. return "导出错误:" . $e->getMessage();
  70. } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
  71. return "导出错误:" . $e->getMessage();
  72. }
  73. }
  74. /**
  75. * 导出的文件标题
  76. * @return string
  77. */
  78. public function title(): string
  79. {
  80. return $this->title;
  81. }
  82. /**
  83. * 标题行
  84. * @return array
  85. */
  86. public function headings(): array
  87. {
  88. return $this->headings;
  89. }
  90. /**
  91. * 导出的内容
  92. * @return \Illuminate\Support\Collection
  93. */
  94. public function collection()
  95. {
  96. return collect($this->data);
  97. }
  98. /**
  99. * 设置单元格事件
  100. * @return array
  101. */
  102. public function registerEvents(): array
  103. {
  104. return [
  105. AfterSheet::Class => function (AfterSheet $event) {
  106. $count = count($this->data);
  107. foreach ($this->typeLists AS $cell => $typeList) {
  108. if ($cell && $typeList) {
  109. $p = $this->headings ? 1 : 0;
  110. for ($i = 1 + $p; $i <= max($count, $this->typeNumber) + $p; $i++) {
  111. $validation = $event->sheet->getDelegate()->getCell($cell . $i)->getDataValidation();
  112. $validation->setType(DataValidation::TYPE_LIST);
  113. $validation->setErrorStyle(DataValidation::STYLE_WARNING);
  114. $validation->setAllowBlank(false);
  115. $validation->setShowDropDown(true);
  116. $validation->setShowInputMessage(true);
  117. $validation->setShowErrorMessage(true);
  118. $validation->setErrorTitle('输入的值不合法');
  119. $validation->setError('选择的值不在列表中,请选择列表中的值');
  120. $validation->setPromptTitle('从列表中选择');
  121. $validation->setPrompt('请选择下拉列表中的值');
  122. $validation->setFormula1('"' . implode(',', $typeList) . '"');
  123. }
  124. }
  125. }
  126. }
  127. ];
  128. }
  129. }