Maatwebsite\Excel导入用法

1、WithChunkReading 分块导入#

导入行数过多时,可以使用 WithChunkReading 将数据拆成小块进行导入,减小内存占用

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;

class IncomeOrderImport implements ToCollection, WithChunkReading
{

    /**
     * @return int
     */

    public function chunkSize(): int
    {
        return 300;
    }

    public function collection(Collection $rows)
    {

    }
}

2、WithStartRow 跳过表头#

导入时 excel 一般都会有表头,如果在 collection 方法中直接过滤表头,第一块数据含表头,第一块之后的不含表头的正常数据会缺失

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithStartRow;

class IncomeOrderImport implements ToCollection, WithChunkReading, WithStartRow
{
    /**
     * @return int
     */
    public function startRow(): int
    {
        return 3;
    }

    /**
     * @return int
     */
    public function chunkSize(): int
    {
        return 300;
    }

    public function collection(Collection $rows)
    {

    }
}

3、使用分块或跳过表头行后,collection 中数据校验时给出对应行的错误提示#

protected $start = 4;//第几行开始导入
protected $size = 100;//分块大小
protected $chunk = 0;//第几块

public function __construct()
{
    //一些关联数据可以直接在这查询,不然在collection中会查询多次
}

/**
 * @return int
 */
public function startRow(): int
{
    return $this->start;
}

/**
 * @return int
 */
public function chunkSize(): int
{
    return $this->size;
}

public function collection(Collection $rows)
{
    $this->chunk = $this->chunk + 1;
    foreach ($rows as $key => $row) {
        $line = $key + $this->size * ($this->chunk - 1) + $this->start;
        if (empty($row[1])) {
            throw new \Exception('第' . $line . "行,订单时间不可为空");
        }
    }
}

4、WithCalculatedFormulas 获取计算公式后的值#

如果有用到计算公式,默认读取到的值为计算公式,使用 WithCalculatedFormulas 可以自动获取计算后的值

namespace App\Imports;

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithCalculatedFormulas;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithStartRow;

class IncomeOrderImport implements ToCollection, WithChunkReading, WithStartRow, WithCalculatedFormulas
{
    /**
     * @return int
     */
    public function startRow(): int
    {
        return 3;
    }

    /**
     * @return int
     */
    public function chunkSize(): int
    {
        return 300;
    }

    public function collection(Collection $rows)
    {

    }
}

5、WithMultipleSheets 多 sheet 读取#

多 sheet,不同 sheet 处理数据不一样是,可以使用 WithMultipleSheets

namespace App\Imports;

use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class IncomeOrderImport implements WithMultipleSheets
{

    /**
     * @return array
     */
    public function sheets(): array
    {
        return [
            'sheet1' => new OrderImport,
        ];
    }

}

6、WithConditionalSheets 读取指定 sheet#

有时候导入模板可能会依赖于多个 sheet 之间做一些数据校验,这样可以导入之前填写时实时进行数据校验,减少人工错误概率,

但这是导入时只需要某个 sheet 的数据,其他辅助 sheet 的数据是不需要的,这是需要过滤 sheet

namespace App\Imports;

use Maatwebsite\Excel\Concerns\WithConditionalSheets;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class IncomeOrderImport implements WithMultipleSheets
{
    use WithConditionalSheets;
    public function conditionalSheets(): array
    {
        return [
            'Sheet1' => new OrderImport,
        ];
    }

}

调用时 onlySheets 方法

Excel::import((new IncomeOrderImport())->onlySheets('Sheet1'), $file);
本作品采用《CC 协议》,转载必须注明作者和本文链接
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。