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 协议》,转载必须注明作者和本文链接
能同时处理多个表关系之间的方法吗?