LDU*_*BBS 13 laravel maatwebsite-excel
我正在尝试使用3.1版导入5.7版中的.xlsx文件。我想要实现的是跳过文件的第一行以避免在我的数据库中导入列标题。LaravelMaatwebsite-excel
我尝试使用版本 2 语法,调用该skip()方法。
public function voter_import(Request $request)
{
if (empty($request->file('file')->getRealPath()))
{
return back()->with('success','No file selected');
}
else
{
Excel::import(new VotersImport, $request->file('file'))->skip(1);
return response('Import Successful, Please Refresh Page');
}
}
class VotersImport implements ToModel
{
public function model(array $row)
{
return new Voter([
'fname' => $row[0],
'lname' => $row[1],
'phone' => $row[2],
'gender' => $row[3],
'state' => $row[4],
'occupation' => $row[5],
'address' => $row[6],
'vin' => $row[7],
'dob' => $row[8],
'campaign_id' => $row[9],
]);
}
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
Call to undefined method Maatwebsite\Excel\Excel::skip()
Rob*_*ard 31
你可以实现开始行
use Maatwebsite\Excel\Concerns\WithStartRow;
class VotersImport implements ToModel, WithStartRow
{
/**
* @return int
*/
public function startRow(): int
{
return 2;
}
}
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用 HeadingRow https://docs.laravel-excel.com/3.1/imports/heading-row.html
| 归档时间: |
|
| 查看次数: |
13596 次 |
| 最近记录: |