目前,我可以使用这种格式从excel文件中获取数据。
这是我的工作代码。
$validator = Validator::make(
[
'file' => $request->file,
'extension' => strtolower($request->file->getClientOriginalExtension()),
],
[
'file' => 'required|max:5000',
'extension' => 'required|in:,csv,xlsx,xls',
]
);
$modal = "active";
if($validator->fails()){
return redirect()
->back()
->with(['errors'=>$validator->errors()->all()])
->with('modal',$modal);
}
$dateTime = date('Ymd_His');
$file = $request->file('file');
$fileName = $dateTime . '-' . $file->getClientOriginalName();
$savePath = public_path('/upload/projectlist/');
$file->move($savePath, $fileName);
$excel = Importer::make('Excel');
$excel->hasHeader(true);
$excel->load($savePath.$fileName);
$collection = $excel->getCollection();
if(sizeof($collection[1]) == 5)
{
$arr = json_decode($collection,true);
foreach ($arr as $row) {
$insert_data[] = array(
'first_name' => $row['first_name'],
'last_name' => $row['last_name'],
'email' => $row['email'],
'birthdate' => $row['birthdate']['date'],
'created_at' => now(),
'updated_at' => now(),
);
}
DB::table('tbl_sampleimport')->insert($insert_data);
}
else
{
return redirect()
->back()
->with(['errors'=> [0=> 'Please provide date in file according to your format.']])
->with('modal',$modal);
}
return redirect()->back()
->with(['success'=>'File uploaded successfully!'])
->with('modal',$modal);
现在,格式已更改。添加了一些具有单个值的字段。像Project Name
和Project Date
我正在努力获取其价值。只是想获取值,但也不必将其保存到数据库。
如何获取Project Name
和Project Date
的值声明为变量。并且仍然能够保存j.doe17
和j.doe18
的数据?
我正在使用网鸭作为laravel / excel进行导入等。