|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PowerComponents\LivewirePowerGrid\Components\Exports\OpenSpout\v5; |
| 4 | + |
| 5 | +use OpenSpout\Common\Entity\Row; |
| 6 | +use OpenSpout\Common\Entity\Style\Style; |
| 7 | +use OpenSpout\Common\Exception\IOException; |
| 8 | +use OpenSpout\Writer\Exception\WriterNotOpenedException; |
| 9 | +use OpenSpout\Writer\XLSX\{Options, Writer}; |
| 10 | +use PowerComponents\LivewirePowerGrid\Components\Exports\Contracts\ExportInterface; |
| 11 | +use PowerComponents\LivewirePowerGrid\Components\Exports\Export; |
| 12 | +use PowerComponents\LivewirePowerGrid\Components\SetUp\Exportable; |
| 13 | +use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 14 | + |
| 15 | +/** @codeCoverageIgnore */ |
| 16 | +class ExportToXLS extends Export implements ExportInterface |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @throws \Exception |
| 20 | + */ |
| 21 | + public function download(Exportable|array $exportOptions): BinaryFileResponse |
| 22 | + { |
| 23 | + $deleteFileAfterSend = boolval(data_get($exportOptions, 'deleteFileAfterSend')); |
| 24 | + $this->striped = strval(data_get($exportOptions, 'striped')); |
| 25 | + |
| 26 | + /** @var array $columnWidth */ |
| 27 | + $columnWidth = data_get($exportOptions, 'columnWidth', []); |
| 28 | + $this->columnWidth = $columnWidth; |
| 29 | + |
| 30 | + $this->build($exportOptions); |
| 31 | + |
| 32 | + return response() |
| 33 | + ->download(storage_path($this->fileName.'.xlsx')) |
| 34 | + ->deleteFileAfterSend($deleteFileAfterSend); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @throws WriterNotOpenedException|IOException |
| 39 | + */ |
| 40 | + public function build(Exportable|array $exportOptions): void |
| 41 | + { |
| 42 | + $stripTags = boolval(data_get($exportOptions, 'stripTags', false)); |
| 43 | + $data = $this->prepare($this->data, $this->columns, $stripTags); |
| 44 | + |
| 45 | + $options = new Options(); |
| 46 | + $writer = new Writer($options); |
| 47 | + |
| 48 | + $writer->openToFile(storage_path($this->fileName.'.xlsx')); |
| 49 | + |
| 50 | + $style = (new Style()) |
| 51 | + ->withFontBold(true) |
| 52 | + ->withFontSize(12) |
| 53 | + ->withShouldWrapText(false) |
| 54 | + ->withBackgroundColor('d0d3d8'); |
| 55 | + |
| 56 | + $row = Row::fromValuesWithStyle($data['headers'], $style); |
| 57 | + |
| 58 | + $writer->addRow($row); |
| 59 | + |
| 60 | + /** |
| 61 | + * @var int<1, max> $column |
| 62 | + * @var float $width |
| 63 | + */ |
| 64 | + foreach ($this->columnWidth as $column => $width) { |
| 65 | + $options->setColumnWidth($width, $column); |
| 66 | + } |
| 67 | + |
| 68 | + $default = (new Style()) |
| 69 | + ->withFontSize(12); |
| 70 | + |
| 71 | + $gray = (new Style()) |
| 72 | + ->withFontSize(12) |
| 73 | + ->withBackgroundColor($this->striped); |
| 74 | + |
| 75 | + /** @var array<string> $row */ |
| 76 | + foreach ($data['rows'] as $key => $row) { |
| 77 | + if (count($row)) { |
| 78 | + if ($key % 2 && $this->striped) { |
| 79 | + $row = Row::fromValuesWithStyle($row, $gray); |
| 80 | + } else { |
| 81 | + $row = Row::fromValuesWithStyle($row, $default); |
| 82 | + } |
| 83 | + $writer->addRow($row); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + $writer->close(); |
| 88 | + } |
| 89 | +} |
0 commit comments