用spout写了导出

60 阅读1分钟
<?php
namespace App\Services\Service\Export;

require './../vendor/autoload.php';

use App\Services\Implement\Export\SpoutInterface;
use Box\Spout\Common\Entity\Row;
use Box\Spout\Common\Entity\Style\Style;
use Box\Spout\Common\Exception\IOException;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Exception\InvalidSheetNameException;
use Box\Spout\Writer\Exception\WriterNotOpenedException;

class SpoutService implements SpoutInterface
{
    private string $title;
    private array $headers = [];
    private iterable $data = [];
    private array $fieldMap;
    public $spoutWriter;
    public Style $spoutStyle;
    public function __construct($fileType){
        if ($fileType === "csv") {
            $this->spoutWriter = WriterEntityFactory::createCSVWriter();
        } elseif ($fileType === "xlsx"||$fileType === "xls") {
            $this->spoutWriter = WriterEntityFactory::createXLSXWriter();
        } elseif ($fileType === "ods") {
            $this->spoutWriter = WriterEntityFactory::createODSWriter();
        }
        $this->spoutStyle = (new StyleBuilder())->setFontBold()->build();
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;
        return $this;
    }

    public function setHeaders(array $headers): self
    {
        $this->headers = $headers;
        return $this;
    }

    public function fillData(iterable $data, array $fieldMap): self
    {
        $this->data = $data;
        $this->fieldMap = $fieldMap;
        return $this;
    }


    /**
     * @throws IOException
     * @throws InvalidSheetNameException
     * @throws WriterNotOpenedException
     */
    public function saveToFile(string $filename): self
    {
        $this->spoutWriter->openToBrowser($filename);
        $this->spoutWriter->getCurrentSheet()->setName($this->title);
        $this->writeData();
        $this->spoutWriter->close();
        exit();
    }

    /**
     * @throws IOException
     * @throws WriterNotOpenedException
     */
    private function writeData()
    {
        $headerRow = WriterEntityFactory::createRowFromArray($this->headers, $this->spoutStyle);
        $this->spoutWriter->addRow($headerRow);

        foreach ($this->generateData() as $row) {
            $this->spoutWriter->addRow($this->createRowWithTextColumns($row));
        }
    }

    private function generateData(): iterable
    {
        foreach ($this->data as $row) {
            $rowArray = [];
            foreach ($this->fieldMap as $key => $value) {
                $rowArray[] = $row[$key] ?? '';
            }
            yield $rowArray;
        }
    }

    private function createRowWithTextColumns(array $rowData): Row
    {
        $cells = [];
        foreach ($rowData as $value) {
            $cell = WriterEntityFactory::createCell($value);
            $cells[] = $cell;
        }

        return WriterEntityFactory::createRow($cells);
    }
}