我正在尝试使用.xlsx
版本3.1导入Laravel
版本5.7中的Maatwebsite-excel
文件。我希望实现的是跳过文件的第一行,以避免在我的数据库中导入列标题。
我尝试使用version 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],
]);
}
}
错误信息:
Call to undefined method Maatwebsite\Excel\Excel::skip()
发布于 2019-06-23 22:58:46
您可以实现StartingRow
use Maatwebsite\Excel\Concerns\WithStartRow;
class VotersImport implements ToModel, WithStartRow
{
/**
* @return int
*/
public function startRow(): int
{
return 2;
}
}
另一种选择是使用HeadingRow https://docs.laravel-excel.com/3.1/imports/heading-row.html
发布于 2019-10-25 00:48:24
在加载Excel文件之前添加此代码。它对我有用。。
config(['excel.import.startRow' => your_number_of_row_to_skip]);
Ex:
$path = $request->file('select_file')->getRealPath();
config(['excel.import.startRow' => 4]);
$data = Excel::load($path)->get();
发布于 2022-06-22 05:20:40
在控制器中使用这样的setter函数
public function voter_import(Request $request)
{
if (empty($request->file('file')->getRealPath()))
{
return back()->with('success','No file selected');
}
else
{
$import = new VotersImport();
$import->setStartRow(2);
Excel::import($import, $request->file('file');
return response('Import Successful, Please Refresh Page');
}
}
在VotersImport类中添加setter函数并关注WithStartRow
private $setStartRow = 1;
public function setStartRow($setStartRow){
$this->setStartRow = $setStartRow;
}
public function startRow() : int{
return $setStartRow;
}
https://stackoverflow.com/questions/56726778
复制相似问题