RowExport.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Admin\Exports;
  3. use Encore\Admin\Grid\Exporters\AbstractExporter;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Collection;
  6. use Maatwebsite\Excel\Concerns\Exportable;
  7. use Maatwebsite\Excel\Concerns\FromCollection;
  8. use Maatwebsite\Excel\Concerns\RegistersEventListeners;
  9. use Maatwebsite\Excel\Concerns\ShouldAutoSize;
  10. use Maatwebsite\Excel\Concerns\WithColumnFormatting;
  11. use Maatwebsite\Excel\Concerns\WithEvents;
  12. use Maatwebsite\Excel\Concerns\WithHeadings;
  13. use Maatwebsite\Excel\Concerns\WithMapping;
  14. use Maatwebsite\Excel\Events\AfterSheet;
  15. use Maatwebsite\Excel\Events\BeforeSheet;
  16. use Maatwebsite\Excel\Events\BeforeWriting;
  17. use PhpOffice\PhpSpreadsheet\Shared\Font;
  18. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  19. abstract class RowExport extends AbstractExporter implements
  20. FromCollection,
  21. WithHeadings,
  22. ShouldAutoSize,
  23. WithColumnFormatting,
  24. WithMapping,
  25. WithEvents
  26. {
  27. use Exportable, RegistersEventListeners;
  28. /**
  29. * @var Collection
  30. */
  31. private $customerData = null;
  32. /**
  33. * @return mixed
  34. */
  35. public function getCustomerData()
  36. {
  37. return $this->customerData;
  38. }
  39. /**
  40. * @param Collection $customerData
  41. * @return $this
  42. */
  43. public function setCustomerData(Collection $customerData)
  44. {
  45. $this->customerData = $customerData;
  46. return $this;
  47. }
  48. /**
  49. * @return Collection
  50. */
  51. public function collection()
  52. {
  53. return $this->getCustomerData()?:$this->getData(false);
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function export()
  59. {
  60. $this->download($this->getFilename())->send();
  61. exit;
  62. }
  63. /**
  64. * 设置标题,返回标题数组
  65. * @return array
  66. */
  67. abstract public function headings(): array ;
  68. /**
  69. * 处理行数据
  70. * @param Model $row
  71. * @return array
  72. */
  73. abstract public function map($row): array ;
  74. /**
  75. * 设置文件名
  76. * @return string
  77. */
  78. abstract public function getFilename(): string ;
  79. /**
  80. * 格式化指定列.
  81. * 只有在需要的时候设置,例如在身份证不要显示科学计数时设定列格式:['A'=>NumberFormat::FORMAT_NUMBER]
  82. * @return array
  83. */
  84. abstract public function columnFormats(): array ;
  85. public static function beforeWriting(BeforeWriting $event)
  86. {
  87. Font::setAutoSizeMethod(Font::AUTOSIZE_METHOD_EXACT);
  88. Font::setTrueTypeFontPath(public_path('fonts/'));
  89. $event->getWriter()->getDelegate()->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  90. $event->getWriter()->getDelegate()->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
  91. }
  92. public static function beforeSheet(BeforeSheet $event)
  93. {
  94. }
  95. public static function afterSheet(AfterSheet $event)
  96. {
  97. //$event->getSheet()->getDelegate()->getColumnDimension('A')->setWidth(50);
  98. }
  99. }