zoukankan      html  css  js  c++  java
  • laravel 中使用maatwebsite/excel导入 不完全的解决方法

    主要是编码问题 把上传的文件做如下处理

    $file = $_FILES;
    $excel_file_path = $file['file']['tmp_name'];
    //文件内容转码
    $content = file_get_contents($excel_file_path);
    $fileType = mb_detect_encoding($content, array('UTF-8', 'GBK', 'LATIN1', 'BIG5'));//获取当前文本编码格
    $res = Excel::load($excel_file_path, function ($reader) {

    }, $fileType)->get()->toArray();
    就能得到 想要的数组 然后存入数据库, 就完成了excel的导入功能
    注意修改的配置excel.php
    'heading' => false//

    'to_ascii' => true


    导入的代码
    前端

    导入EXCEL添加学生  

    1. <form action="/admin/student/import" method='post' enctype="multipart/form-data">  
    2. <input id="fileId1" type="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" name="file"/>   
    3. <input type="submit" value="确认">  
    4. </form>
    5. 后端代码如下
      1. public function import(Request $request){  
      2.   
      3.        if(!$request->hasFile('file')){  
      4.            exit('上传文件为空!');  
      5.        }  
      6.        $file = $_FILES;  
      7.        $excel_file_path = $file['file']['tmp_name'];  
      8.     $res = [];    
      9.        Excel::load($excel_file_path, function($reader) use( &$res ) {    
      10.            $reader = $reader->getSheet(0);    
      11.            $res = $reader->toArray();    
      12.        });  
      13.        for($i = 1;$i<count($res);$i++){  
      14.            $check = Students::where('name',$res[$i][0])->where('title',$res[$i][4])->count();  
      15.            if($check){  
      16.                continue;  
      17.            }  
      18.            $stu = new Students;  
      19.            $stu->name = $res[$i][0];  
      20.            $stu->group = $res[$i][1];  
      21.            $stu->teacher = $res[$i][2];  
      22.            $stu->school = $res[$i][3];  
      23.            $stu->mobile = $res[$i][4];  
      24.            $stu->title = $res[$i][5];  
      25.            $stu->save();  
      26.        }  
      27.        return Redirect::to('/admin/student')->withSuccess("导入成功");  
      28.          
      29.    }  
        
  • 相关阅读:
    SpringMVC学习笔记----
    各种语言学习
    七步精通Python机器学习 转
    开源GIS有用
    sql 语句
    调试windows服务最简单的方法之一
    sqlserver2008 win7下安装 windows installer错误
    高德地图AmapSDKDemo运行
    google mapsDemo示例运行
    android开发环境搭建遇到的问题
  • 原文地址:https://www.cnblogs.com/chen1970s/p/7122213.html
Copyright © 2011-2022 走看看