【问题标题】:Laravel How to upload EXCEL file to database?Laravel如何将EXCEL文件上传到数据库?
【发布时间】:2018-04-10 06:40:06
【问题描述】:

我的 Laravel 版本是 5.6,PHP 是 7。 我在互联网上找到了一些上传示例,但大多数都不起作用。 有没有人可以给我一个完整的例子? 我在网上找到了一个类似的案例。 Laravel 5.6 Excel and CSV import export using maatwebsite example

不幸的是,它也不起作用。

这是我的源代码。 刀片.php

<form class="form-upload" method="POST" action="{{url('/excelUpload')}}" enctype="multipart/form-data">
            {{ csrf_field() }}
             <div class="form-group">
                <label for="file">excel upload</label>
                <input type="file" id="file" name="ex_file">
                <p class="help-block">!!!</p>
             </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default">cancel</button>
                <button type="submit" class="btn btn-primary" id="upload_f">submit</button>

controller.php

public function excelUpload(Request $request){

         if($request->hasFile('StudentUploadSample')){
             return "yes i have a file";
         }
         return "nofile";
    }

但是,结果总是显示“nofile”。 我没有使用任何“use”。我应该使用一些东西吗? 网页上的示例也没有使用任何“use”。 另外,我看了一些网站说文件会放在storage文件夹下,但是在storage文件夹下找不到我的文件“StudentUploadSample.xlsx”。

有没有人可以给我一个完整的例子或者如何解决这个问题?

【问题讨论】:

  • 它将返回“nofile”,因为您的输入名称与您的请求不同。您的输入名称是ex_file,但您请求的是StudentUploadSample。它将返回“nofile”
  • 投票赞成关闭,因为它有拼写错误。您应该使用您的文件名StudentUploadSample 或在ex_file 上致电hasFile

标签: php excel laravel upload


【解决方案1】:

替换文件名并使用参考站点,您将在其中获得正确的代码和解释。

参考网站如:http://www.expertphp.in/article/laravel-5-maatwebsite-import-excel-into-db-and-export-data-into-csv-and-excel

public function importFileIntoDB(Request $request){
    if($request->hasFile('sample_file')){
        $path = $request->file('sample_file')->getRealPath();
        $data = \Excel::load($path)->get();
        if($data->count()){
            foreach ($data as $key => $value) {
                $arr[] = ['name' => $value->name, 'details' => $value->details];
            }
            if(!empty($arr)){
                \DB::table('products')->insert($arr);
                dd('Insert Record successfully.');
            }
        }
    }
    dd('Request data does not have any files to import.');      
} 

【讨论】:

  • 是的,它工作,谢谢。但是......新问题......错误消息显示此“调用未定义的方法Maatwebsite\Excel\Excel::load()”
  • 非常感谢
【解决方案2】:

您在控制器中使用了错误的文件名:

if($request->hasFile('ex_file')){
     return "yes i have a file";
  }

所以修复它,然后尝试检查。

【讨论】:

  • 是的,它工作,谢谢。但是......新问题......错误消息显示这个“调用未定义的方法Maatwebsite\Excel\Excel::load()”你知道吗
【解决方案3】:

&lt;input type="file" id="file" name="ex_file"&gt; 替换为&lt;input type="file" id="file" name="StudentUploadSample"&gt;

你就完成了。

【讨论】:

  • 是的,它有效,谢谢。但是......新问题......错误消息显示这个“调用未定义的方法Maatwebsite\Excel\Excel::load()”你知道吗
【解决方案4】:

你可以使用 laravel excel。

地址:https://github.com/Maatwebsite/Laravel-Excel

/** * 我的控制器代码 * */

$updateFile = $request->file('update_file');

$fileSize = (int) 文件大小($updateFile);

$fileExtension = $updateFile->getClientOriginalExtension();

$filePath = $updateFile->getRealPath();

$excelData = Excel::load($filePath)->get()->toArray();

或者您可以使用:https://phpspreadsheet.readthedocs.io/en/develop/

【讨论】:

  • 啊...我正在使用它
猜你喜欢
  • 2013-12-24
  • 2017-04-18
  • 2015-10-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-24
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
相关资源
最近更新 更多