【问题标题】:Laravel 5.3 import CSV from file without uploadingLaravel 5.3从文件导入CSV而不上传
【发布时间】:2016-12-07 10:46:28
【问题描述】:

我正在使用 Maatwebsite/Excel 导入 CSV 文件。我的代码看起来像这个教程:http://itsolutionstuff.com/post/laravel-5-import-export-to-excel-and-csv-using-maatwebsite-exampleexample.html 我标记了两个函数,我的脚本获取文件并返回文件/

但尽管尝试了很多次,我还是无法跳过上传文件。这意味着,我在特殊文件夹中有 CSV 文件,我想让这个文件来处理数据,而不上传。它会自动获取文件(可能是 file_get_contents ?),并将其传递给 Illuminate\Support\Facades\Input。

【问题讨论】:

  • 警惕内存限制。如果有人上传了一个 250 MB 的 .xlsx 文件,您可能无法在不点击死机白屏并首先丢失数据的情况下处理该文件。

标签: laravel csv laravel-5 file-get-contents maatwebsite-excel


【解决方案1】:

这是在 laravel 5 中导入 csv 的完美方式。*

 public function importExcel() {
    if (Input::hasFile('import_file')) {
        $path = Input::file('import_file')->getRealPath();
        $data = Excel::load($path, function($reader) {

                })->get();
        if (!empty($data) && $data->count()) {
            $count = 0;
            foreach ($data as $key => $d) {
                $insert = array();
                $insert['name'] = $value->name;
                $this->user->create($insert);
            }
        }
    }
}

【讨论】:

  • 如果回答有帮助,请标记为有用:)
  • 什么是 Excel 类?
【解决方案2】:

您可以使用此代码读取您的 excel 文件

    // for sheet 1
$excelData = $excel->selectSheetsByIndex(0)->load($yourFilePath)->get();
print_r($excelData);

【讨论】:

  • $excel 变量是什么?我可以把这段代码放在哪里?
  • 需要在模型中注入的excel变量,比如这个公共函数uploadVerifications(Excel $excel){}
【解决方案3】:

我找到了解决方案。我们可以跳过设置为 CSV 文件路径的 if (Input::hasFile('import_file')) {$path 变量

【讨论】:

    猜你喜欢
    • 2017-02-23
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 1970-01-01
    • 2017-04-13
    • 2013-07-03
    • 2017-09-03
    相关资源
    最近更新 更多