123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace App\Admin\Exports;
- use Encore\Admin\Grid\Exporters\AbstractExporter;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Support\Collection;
- use Maatwebsite\Excel\Concerns\Exportable;
- use Maatwebsite\Excel\Concerns\FromCollection;
- use Maatwebsite\Excel\Concerns\RegistersEventListeners;
- use Maatwebsite\Excel\Concerns\ShouldAutoSize;
- use Maatwebsite\Excel\Concerns\WithColumnFormatting;
- use Maatwebsite\Excel\Concerns\WithEvents;
- use Maatwebsite\Excel\Concerns\WithHeadings;
- use Maatwebsite\Excel\Concerns\WithMapping;
- use Maatwebsite\Excel\Events\AfterSheet;
- use Maatwebsite\Excel\Events\BeforeSheet;
- use Maatwebsite\Excel\Events\BeforeWriting;
- use PhpOffice\PhpSpreadsheet\Shared\Font;
- use PhpOffice\PhpSpreadsheet\Style\Alignment;
- abstract class RowExport extends AbstractExporter implements
- FromCollection,
- WithHeadings,
- ShouldAutoSize,
- WithColumnFormatting,
- WithMapping,
- WithEvents
- {
- use Exportable, RegistersEventListeners;
- /**
- * @var Collection
- */
- private $customerData = null;
- /**
- * @return mixed
- */
- public function getCustomerData()
- {
- return $this->customerData;
- }
- /**
- * @param Collection $customerData
- * @return $this
- */
- public function setCustomerData(Collection $customerData)
- {
- $this->customerData = $customerData;
- return $this;
- }
- /**
- * @return Collection
- */
- public function collection()
- {
- return $this->getCustomerData()?:$this->getData(false);
- }
- /**
- * {@inheritdoc}
- */
- public function export()
- {
- $this->download($this->getFilename())->send();
- exit;
- }
- /**
- * 设置标题,返回标题数组
- * @return array
- */
- abstract public function headings(): array ;
- /**
- * 处理行数据
- * @param Model $row
- * @return array
- */
- abstract public function map($row): array ;
- /**
- * 设置文件名
- * @return string
- */
- abstract public function getFilename(): string ;
- /**
- * 格式化指定列.
- * 只有在需要的时候设置,例如在身份证不要显示科学计数时设定列格式:['A'=>NumberFormat::FORMAT_NUMBER]
- * @return array
- */
- abstract public function columnFormats(): array ;
- public static function beforeWriting(BeforeWriting $event)
- {
- Font::setAutoSizeMethod(Font::AUTOSIZE_METHOD_EXACT);
- Font::setTrueTypeFontPath(public_path('fonts/'));
- $event->getWriter()->getDelegate()->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
- $event->getWriter()->getDelegate()->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
- }
- public static function beforeSheet(BeforeSheet $event)
- {
- }
- public static function afterSheet(AfterSheet $event)
- {
- //$event->getSheet()->getDelegate()->getColumnDimension('A')->setWidth(50);
- }
- }
|