【发布时间】:2019-11-27 05:55:05
【问题描述】:
目前,我可以从这种格式的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的数据吗?
我使用cyber-duck作为我的laravel/excel进行导入等等。
【问题讨论】:
-
您的导入器类是做什么的?它是您制作的自定义类还是库。?
-
你以前用过
phpspreadsheet吗? -
我建议您在同一个 excel 中为元数据使用其他工作表,因为如果不是,您将无法将您的收藏作为库获取,无法解析标题并且无法为您提供键值对数据。
-
任何库都会有同样的限制。如果您需要收集,则数据应类似于标题行和数据行。到目前为止,我还没有找到任何解决方案,我们可以跳过五行然后获取集合。
-
我会选择第一个选项来分隔工作表。
标签: php excel laravel cyberduck