我正在尝试导入一个.xlsx
文件输入Laravel
5.7版本使用Maatwebsite-excel
版本 3.1。我想要实现的是跳过文件的第一行,以避免在数据库中导入列标题。
我尝试使用版本 2 语法,调用skip()
method.
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],
]);
}
}
错误信息:
Call to undefined method Maatwebsite\Excel\Excel::skip()
你可以实现StartingRow
use Maatwebsite\Excel\Concerns\WithStartRow;
class VotersImport implements ToModel, WithStartRow
{
/**
* @return int
*/
public function startRow(): int
{
return 2;
}
}
另一种选择是使用 HeadingRowhttps://docs.laravel-excel.com/3.1/imports/heading-row.html https://docs.laravel-excel.com/3.1/imports/heading-row.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)