【问题标题】:”maatwebsite/excel 3.1“ how to filter the excel's title when import & export the excel《maatwebsite/excel 3.1》 导入导出excel时如何过滤excel的标题
【发布时间】:2020-04-03 14:34:05
【问题描述】:

我想将excel导入mysql 这是我的excel格式 enter image description here

但我发现我无法过滤此 excel 的标题“名称”&&“id_number” 这是我第一次使用“maatwebsite/excel”3.1 我不知道怎么用

请帮帮我 谢谢 这是我的代码

namespace App\Imports;

use App\Models\Person;
use Maatwebsite\Excel\Concerns\ToModel;

class PersonImport implements ToModel
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Person([
            'name' => $row[0],
            'id_number' => $row[1],
            'status' => 1
        ]);
    }

}

这是我的控制器功能

 $import = new PersonImport;
 $result =  Excel::import($import, $request->file('file'));

【问题讨论】:

  • 你应该使用标题行覆盖

标签: excel laravel import export


【解决方案1】:

你可以实现 WithHeadingRow

https://docs.laravel-excel.com/3.1/imports/heading-row.html#heading-row

您的代码将是:

namespace App\Imports;

use App\Models\Person;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class PersonImport implements ToModel, WithHeadingRow
{
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        return new Person([
            'name' => $row['name'],
            'id_number' => $row['id_number'],
            'status' => 1
        ]);
    }

}

【讨论】:

  • 但我不需要这样的标题。
猜你喜欢
  • 1970-01-01
  • 2018-10-23
  • 2022-11-19
  • 2020-07-09
  • 2019-05-20
  • 2020-03-10
  • 1970-01-01
  • 2020-08-28
  • 2016-11-23
相关资源
最近更新 更多