乐趣区

关于laravel:Laravel6配合Maatwebsite\Excel-实现-Excel-导入

前一段须要我的项目中须要通过 Excel 导入用户,之前用过 phpexcel,总感觉太过繁琐,印象中 phpexcel 也很久没更新,看到我的项目中有应用Maatwebsite\Excel,便尝试应用一下。

装置

composer require maatwebsite/excel

导入

生成导入类

php artisan make:import AdminsImport --model=Admin

会看到 app 上面生成了 Imports 文件夹。

欠缺业务逻辑

<?php

namespace App\Imports;

use App\Models\Admin;
use function EasyWeChat\Kernel\Support\str_random;
use Maatwebsite\Excel\Concerns\ToModel;

class AdminsImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        // 过滤表头和空行,我这边表头的第一个单元格是 id,具体自行调整
        if (empty($row[0]) || $row[0] == 'id') {return null;}
        return new Admin(['username' => $row[2],
            'password' => bcrypt($row[3]),
            'api_token' => str_random(60),
        ]);
    }
}

导入工作

<?php

namespace App\Console\Commands;

use App\Imports\AdminsImport;
use Illuminate\Console\Command;
use Maatwebsite\Excel\Facades\Excel;

class ImportAdmin extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'importAdmin';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入 admin';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {Excel::import(new AdminsImport(), storage_path('files/export.xlsx'));
        $this->info($this->description.'实现');
    }
}

其余逻辑

当然,可能业务必不仅仅是写入数据,可能有一些设计具体业务的操作,那么你能够这样操作。

<?php

namespace App\Imports;

use App\Models\Admin;
use function EasyWeChat\Kernel\Support\str_random;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Collection;


class AdminsImport implements ToCollection
{public function collection(Collection $rows)
    {
        // 如果须要去除表头
        unset($rows[0]);
        //$rows 是数组格局
        return $this->createData($rows);
    }

    public function createData($rows)
    {
        $success = 0;
        foreach ($rows as $row) {$row[0] = (int) $row[0];
            if (empty($row[0])) {continue;}

            (new Admin())->create(
                ['username' => $row[2],
                    'name' => $row[2],
                    'password' => bcrypt($row[3]),
                    'api_token' => str_random(60),
                ]
            );

            // 其余业务代码
            $success++;
        }

        return $success.'-'.count($rows);
    }

}

执行

php7.2 artisan importAdmin

总的来说,应用起来还是简单明了的。

more

具体导入实现能够搜寻 Maatwebsite\Excel\Excel 查看,外面还有导出、以队列形式导入等,反对的格局也是多种多样,具体代码如下,性能还是很弱小的,足够应酬日常需要了。

<?php

namespace Maatwebsite\Excel;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Files\Filesystem;
use Maatwebsite\Excel\Files\TemporaryFile;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\PendingDispatch;
use Maatwebsite\Excel\Helpers\FileTypeDetector;

class Excel implements Exporter, Importer
{
    use RegistersCustomConcerns;

    const XLSX     = 'Xlsx';

    const CSV      = 'Csv';

    const TSV      = 'Csv';

    const ODS      = 'Ods';

    const XLS      = 'Xls';

    const SLK      = 'Slk';

    const XML      = 'Xml';

    const GNUMERIC = 'Gnumeric';

    const HTML     = 'Html';

    const MPDF     = 'Mpdf';

    const DOMPDF   = 'Dompdf';

    const TCPDF    = 'Tcpdf';

    /**
     * @var Writer
     */
    protected $writer;

    /**
     * @var QueuedWriter
     */
    protected $queuedWriter;

    /**
     * @var Filesystem
     */
    protected $filesystem;

    /**
     * @var Reader
     */
    private $reader;

    /**
     * @param Writer       $writer
     * @param QueuedWriter $queuedWriter
     * @param Reader       $reader
     * @param Filesystem   $filesystem
     */
    public function __construct(
        Writer $writer,
        QueuedWriter $queuedWriter,
        Reader $reader,
        Filesystem $filesystem
    ) {
        $this->writer       = $writer;
        $this->reader       = $reader;
        $this->filesystem   = $filesystem;
        $this->queuedWriter = $queuedWriter;
    }

    /**
     * {@inheritdoc}
     */
    public function download($export, string $fileName, string $writerType = null, array $headers = [])
    {return response()->download($this->export($export, $fileName, $writerType)->getLocalPath(),
            $fileName,
            $headers
        )->deleteFileAfterSend(true);
    }

    /**
     * {@inheritdoc}
     */
    public function store($export, string $filePath, string $diskName = null, string $writerType = null, $diskOptions = [])
    {if ($export instanceof ShouldQueue) {return $this->queue($export, $filePath, $diskName, $writerType, $diskOptions);
        }

        $temporaryFile = $this->export($export, $filePath, $writerType);

        $exported = $this->filesystem->disk($diskName, $diskOptions)->copy(
            $temporaryFile,
            $filePath
        );

        $temporaryFile->delete();

        return $exported;
    }

    /**
     * {@inheritdoc}
     */
    public function queue($export, string $filePath, string $disk = null, string $writerType = null, $diskOptions = [])
    {$writerType = FileTypeDetector::detectStrict($filePath, $writerType);

        return $this->queuedWriter->store(
            $export,
            $filePath,
            $disk,
            $writerType,
            $diskOptions
        );
    }

    /**
     * {@inheritdoc}
     */
    public function raw($export, string $writerType)
    {$temporaryFile = $this->writer->export($export, $writerType);

        $contents = $temporaryFile->contents();
        $temporaryFile->delete();

        return $contents;
    }

    /**
     * {@inheritdoc}
     */
    public function import($import, $filePath, string $disk = null, string $readerType = null)
    {$readerType = FileTypeDetector::detect($filePath, $readerType);
        $response   = $this->reader->read($import, $filePath, $readerType, $disk);

        if ($response instanceof PendingDispatch) {return $response;}

        return $this;
    }

    /**
     * {@inheritdoc}
     */
    public function toArray($import, $filePath, string $disk = null, string $readerType = null): array
    {$readerType = FileTypeDetector::detect($filePath, $readerType);

        return $this->reader->toArray($import, $filePath, $readerType, $disk);
    }

    /**
     * {@inheritdoc}
     */
    public function toCollection($import, $filePath, string $disk = null, string $readerType = null): Collection
    {$readerType = FileTypeDetector::detect($filePath, $readerType);

        return $this->reader->toCollection($import, $filePath, $readerType, $disk);
    }

    /**
     * {@inheritdoc}
     */
    public function queueImport(ShouldQueue $import, $filePath, string $disk = null, string $readerType = null)
    {return $this->import($import, $filePath, $disk, $readerType);
    }

    /**
     * @param object      $export
     * @param string|null $fileName
     * @param string      $writerType
     *
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @return TemporaryFile
     */
    protected function export($export, string $fileName, string $writerType = null): TemporaryFile
    {$writerType = FileTypeDetector::detectStrict($fileName, $writerType);

        return $this->writer->export($export, $writerType);
    }
}

最初,感激上面这一篇站内文章让我疾速上手。

  • maatwebsite/Excel 3.1 应用教程(导入篇)

最初,附上 Laravel Excel 文档:

  • Laravel Excel
退出移动版