Laravel 如何将 EXCEL 文件上传到数据库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49747025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel How to upload EXCEL file to database?
提问by Zong
My Laravel version is 5.6.and PHP is 7. I found some upload sample on the internet, but most of them do not work. Have anyone can provide a complete example to me? I found a similar case on the Internet. Laravel 5.6 Excel and CSV import export using maatwebsite example
我的 Laravel 版本是 5.6.PHP 是 7。我在网上找到了一些上传示例,但大多数都不起作用。有没有人可以提供一个完整的例子给我?我在网上找到了一个类似的案例。 Laravel 5.6 Excel 和 CSV 导入导出使用 maatwebsite 示例
Unfortunately, it doesn't work, too.
不幸的是,它也不起作用。
Here is my source code. blade.php
这是我的源代码。刀片.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
控制器.php
public function excelUpload(Request $request){
if($request->hasFile('StudentUploadSample')){
return "yes i have a file";
}
return "nofile";
}
However, the result always show "nofile". I didn't use any "use". Should I use something? The example on the web page does not use any "use", too. In addition, I read some of the websites that the files will be placed under the storage folder, but I can't find my file "StudentUploadSample.xlsx" in the storage folder.
但是,结果总是显示“ nofile”。我没有使用任何“使用”。我应该使用一些东西吗?网页上的示例也没有使用任何“使用”。另外,我看了一些网站说文件会放在storage文件夹下,但是在storage文件夹里找不到我的文件“StudentUploadSample.xlsx”。
Have anyone can provide a complete example to me or how to solve this problem?
有没有人可以向我提供一个完整的例子或如何解决这个问题?
回答by Pavel
You use the wrong name of the file in your controller:
您在控制器中使用了错误的文件名称:
if($request->hasFile('ex_file')){
return "yes i have a file";
}
So fix that, and try to check.
所以解决这个问题,并尝试检查。
回答by dekts
Replace the name of file and use the reference site where you will get the proper code with explanation.
替换文件名并使用参考站点,您将在其中获得带有解释的正确代码。
Reference site like: http://www.expertphp.in/article/laravel-5-maatwebsite-import-excel-into-db-and-export-data-into-csv-and-excel
参考站点如: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.');
}
回答by shubiao-yao
you can use laravel excel.
你可以使用laravel excel。
address:https://github.com/Maatwebsite/Laravel-Excel
地址:https://github.com/Maatwebsite/Laravel-Excel
/** * my Controller code * */
/** * 我的控制器代码 * */
$updateFile = $request->file('update_file');
$updateFile = $request->file('update_file');
$fileSize = (int) filesize($updateFile);
$fileSize = (int) 文件大小($updateFile);
$fileExtension = $updateFile->getClientOriginalExtension();
$fileExtension = $updateFile->getClientOriginalExtension();
$filePath = $updateFile->getRealPath();
$filePath = $updateFile->getRealPath();
$excelData = Excel::load($filePath)->get()->toArray();
$excelData = Excel::load($filePath)->get()->toArray();
or you can use: https://phpspreadsheet.readthedocs.io/en/develop/
回答by Chirag Patel
replace <input type="file" id="file" name="ex_file">
with <input type="file" id="file" name="StudentUploadSample">
替换<input type="file" id="file" name="ex_file">
为<input type="file" id="file" name="StudentUploadSample">
and you are done.
你就完成了。