Laravel 上传excel,读取并写入数据库 (实现自动建表、存记录值

举报
lxw1844912514 发表于 2022/03/28 01:30:02 2022/03/28
【摘要】 <?php namespace App\Http\Controllers; use Illuminate\Foundation\Bus\DispatchesJobs;use Illuminate\Routing\Controller as BaseController;use Illuminate\Foundatio...

    
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Foundation\Bus\DispatchesJobs;
  4. use Illuminate\Routing\Controller as BaseController;
  5. use Illuminate\Foundation\Validation\ValidatesRequests;
  6. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  7. use Illuminate\Support\Facades\Input;
  8. use Maatwebsite\Excel\Facades\Excel;
  9. use Illuminate\Database\Schema\Blueprint;
  10. use Illuminate\Support\Facades\Schema;
  11. use Illuminate\Support\Facades\DB;
  12. class QueryController extends BaseController
  13. {
  14. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  15. public function index(){
  16. $file = Input::file('myfile');
  17. if($file){
  18. // $realPath = $file
  19. // $path = $file -> move(app_path().'/storage/uploads');
  20. $realPath = $file->getRealPath();
  21. $entension = $file -> getClientOriginalExtension(); //上传文件的后缀.
  22. $tabl_name = date('YmdHis').mt_rand(100,999);
  23. $newName = $tabl_name.'.'.'xls';//$entension;
  24. $path = $file->move(base_path().'/uploads',$newName);
  25. $cretae_path = base_path().'/uploads/'.$newName;
  26. //dd($cretae_path);
  27. //dd($file);
  28. Excel::load($cretae_path, function($reader) use($tabl_name){
  29. //$data = $reader->all();
  30. //获取excel的第几张表
  31. $reader = $reader->getSheet(0);
  32. //获取表中的数据
  33. $data = $reader->toArray();
  34. $result = $this->create_table($tabl_name,$data);
  35. dd($result);
  36. //dd($data);
  37. });
  38. }
  39. return view('query.index');
  40. }
  41. public function create_table($table_name,$arr_field)
  42. {
  43. $tmp = $table_name;
  44. $va = $arr_field;
  45. Schema::create("$tmp", function(Blueprint $table) use ($tmp,$va)
  46. {
  47. $fields = $va[0]; //列字段
  48. //$fileds_count = 0; //列数
  49. $table->increments('id');//主键
  50. foreach($fields as $key => $value){
  51. if($key == 0){
  52. $table->string($fields[$key]);//->unique(); 唯一
  53. }else{
  54. $table->string($fields[$key]);
  55. }
  56. //$fileds_count = $fileds_count + 1;
  57. }
  58. });
  59. $value_str= array();
  60. $id = 1;
  61. foreach($va as $key => $value){
  62. if($key != 0){
  63. $content = implode(",",$value);
  64. $content2 = explode(",",$content);
  65. foreach ( $content2 as $key => $val ) {
  66. $value_str[] = "'$val'";
  67. }
  68. $news = implode(",",$value_str);
  69. $news = "$id,".$news;
  70. DB::insert("insert into db_$tmp values ($news)");
  71. //$value_str = '';
  72. $value_str= array();
  73. $id = $id + 1;
  74. }
  75. }
  76. return 1;
  77. }
  78. }

 https://blog.csdn.net/a9925/article/details/51201405

文章来源: blog.csdn.net,作者:lxw1844912514,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/lxw1844912514/article/details/100028141

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。