使用Excel :: import在Maatwebsite / Laravel Excel 3.1上导入CSV

时间:2019-03-06 15:19:59

标签: php laravel maatwebsite-excel laravel-excel

我已经将Maatwebsite / Laravel-Excel从2.1升级到3.1。不推荐使用某些方法。

这是2.1版的代码。

$data = Excel::load($path, function($reader) {})->get()->toArray();

当我使用2.1时它可以工作,但是升级后出现错误

  

调用未定义的方法Maatwebsite \ Excel \ Excel :: load()

对于3.1版,我尝试更改为

$data = Excel::import($path, function($reader) {})->get()->toArray();

我知道这可能不是正确的语法。

使用2.1开发时,这里是我的完整代码。

$path = $request->file('csv_file')->getRealPath();

    if ($request->has('header')) {
        $data = Excel::import($path, function($reader) {})->get()->toArray();
    } else {
        $data = array_map('str_getcsv', file($path));
    }

    if (count($data) > 0) {
        if ($request->has('header')) {
            $csv_header_fields = [];
            foreach ($data[0] as $key => $value) {
                $csv_header_fields[] = $key;
            }
        }
        $csv_data = array_slice($data, 0, 8);

        $csv_data_file = CsvData::create([
            'csv_filename' => $request->file('csv_file')->getClientOriginalName(),
            'csv_header' => $request->has('header'),
            'csv_data' => json_encode($data)
        ]);
    } else {
        return redirect()->back();
    }

    return view('import.import_field', compact( 'csv_header_fields', 'csv_data', 'csv_data_file'));

如何在3.1版上修复此错误?

3 个答案:

答案 0 :(得分:1)

实际上,不需要在Maatwebsite / Laravel-Excel版本3中为Excel导入创建任何额外的类。基本上,您几乎可以像在版本2中一样进行整个CSV到数组的转换:

    $path = $request->file('csv_file')->getRealPath();
    $data = \Excel::toArray('', $path, null, \Maatwebsite\Excel\Excel::TSV)[0];

答案 1 :(得分:0)

import()方法的第一个参数不再是3.1中文件的路径,而是您必须创建的Import文件的类名。

在此导入文件中,您可以定义如何读取图纸以及将其转换为哪种格式,例如模型或集合。

从2.1迁移到3.1的最简单方法是通过运行(在您的情况下)创建此类Import类:

php artisan make:import CsvDataImport 

如果要一次读取所有行,如问题代码中所示,可以在导入文件中使用ToCollection关注点:

use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class CsvDataImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        // Use the $rows collection to create your CsvData model.
    }
}

在您的Controller中,您可以像这样调用导入:

use App\Imports\CsvDataImport;
use Maatwebsite\Excel\Excel;    

Excel::import(new CsvDataImport, $path);

您可以阅读有关导入集合here

的更多信息

答案 2 :(得分:0)

不要降级,可以通过最简单的导入方法轻松使用新的更新版本(“ maatwebsite / excel”:“〜3.1.0” )。

更新您的Composer.json。

 "require": {
    **"maatwebsite/excel": "~3.1.0"**
},

使用新版本的步骤。.

  • 创建导入以在cmd中运行此命令

php artisan make:import UsersImport

  • 它将在app \ imports \ UserImport.php中创建导入

    <?php
    namespace App\Imports;
    
    use Illuminate\Support\Collection;
    use Maatwebsite\Excel\Concerns\ToCollection;
    use Maatwebsite\Excel\Concerns\WithHeadingRow;
    
    class UserImport implements ToCollection,WithHeadingRow
    {
    
    public function collection(Collection $rows)
    {
    
        return $rows;
    
    }
    
    // headingRow function is use for specific row heading in your xls file
    public function headingRow(): int
    {
        return 3;
    }
    }
    ?>
    
  • 在您的控制器文件中。

     use Excel;
    
    public function importExcel(Request $request) {
    
     $import = new UsersImport();
     $data = \Excel::import($import, request()->file('import_file'));
    
     }