在Laravel / Cyber​​-duck excel

时间:2019-11-27 05:55:05

标签: php excel laravel cyberduck

目前,我可以使用这种格式从excel文件中获取数据。

enter image description here

这是我的工作代码。

        $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 NameProject Date 我正在努力获取其价值。只是想获取值,但也不必将其保存到数据库。

enter image description here

如何获取Project NameProject Date的值声明为变量。并且仍然能够保存j.doe17j.doe18的数据?

我正在使用网鸭作为laravel / excel进行导入等。

1 个答案:

答案 0 :(得分:0)

@Dhaval Purohit建议的解决方案

创建工作表并定义setSheet($sheetNumber)

enter image description here

所以我试图只在这种格式的同一excel文件中创建新工作表

enter image description here

这会给我这样的

[{
      "project_name":"Creator Doe",
      "project_date":"7/9/2019"
}]