PHPexcel(ThinkPHP5.0框架)批量導入mysql數據庫

1.下載PHPexcle類庫,放到extends 目錄下

前端代碼

    <form id="upload" action="import_excel" method="post" enctype="multipart/form-data">
        導入商會信息:<input type="file" name="file" id="file">   <input type="submit" name="submit" value="上傳EXcel文檔" />
    </form>

後端代碼

   //導入excel文檔
    public function import_excel(){   
        header('Content-type: text/html; charset=utf-8'); //設置頁面編碼
        set_time_limit(0);
        ini_set('memory_limit','1024M');
        $save_path = "./uploadfile/excle/";
        if(!is_dir($save_path)){
            mkdir($save_path, 0777, true ); //不存創建新的文件夾
        }       
        if(!is_writable($save_path)){       //給目錄存在沒有權限
            chmod($save_path, 0777);
        }
        if(!empty($_FILES)){
            $name = $_FILES['file']['name'];
            $size = $_FILES['file']['size'];
            $name_tmp = $_FILES['file']['tmp_name'];
            $type = explode('.',$name);
            $allow_type = array('xls', 'xlsx', 'csv');
            if (empty($name)) {
                $this->error('您未選擇文件!');
            }
            if(!in_array(strtolower($type[1]),$allow_type)){
                $this->error('上傳的文件格式不正確~');
            }
            $file_name = time() . rand(1000000, 9999999);
            $file_path = $save_path.$file_name .'.'. $type[1]; //文件路徑
            if(move_uploaded_file($name_tmp, $file_path)){             
                require_once "./extend/PHPExcel/IOFactory.php";
                if($type[1] =='xlsx'||$type=='xls' ){
                    $reader = \PHPExcel_IOFactory::createReader('Excel2007');
                }else{
                    $reader = \PHPExcel_IOFactory::createReader('CSV');
                }                   
                $PHPExcel = $reader->load($file_path);
                $objWorksheet = $PHPExcel->getSheet(0);
                $highestRow   = $objWorksheet->getHighestRow();    //取得總行數                
                $highestColumn = $objWorksheet->getHighestColumn();
                $highestColumn = $this->columnIndexFromString($highestColumn);    // 取得總列數轉成數字  
                if($highestRow>5000){                    
                    $this->error('數據太多,請分批次導入(每次小於5000條');
                }
                $data = array();
                $string='';
                $str = "INSERT INTO xr_ccoic(`ccoic_name`,`president`,`vice_president`,`profile`) VALUES ";          
                for ($row =2; $row <= $highestRow; $row++) {//去除標題
                    for ($column =0;$column<$highestColumn;$column++){                     
                        $val = $objWorksheet->getCellByColumnAndRow($column, $row)->getValue();
                        $string .= "'$val'".',';
                    }
                    $str .='('.trim($string,',').'),';
                    $string=''; //清空值
                }   
                $str = trim($str,',').';';
                $result = db()->query($str);
                if($result > 0){
                    $this->success('數據導入成功!');
                }else{
                    $this->error('數據導入失敗!');
                }              
               // $result = db()->query();   
                unlink($file_path);                          
            }
        }
    }
        
        
    //將列名字母轉換成數字
    public  function columnIndexFromString($pString = 'A'){
        static $_indexCache = array();
        if (isset($_indexCache[$pString])) {
            return $_indexCache[$pString];
        }
        static $_columnLookup = array(
            'A' => 1, 'B' => 2, 'C' => 3, 'D' => 4, 'E' => 5, 'F' => 6, 'G' => 7, 'H' => 8, 'I' => 9, 'J' => 10, 'K' => 11, 'L' => 12, 'M' => 13,
            'N' => 14, 'O' => 15, 'P' => 16, 'Q' => 17, 'R' => 18, 'S' => 19, 'T' => 20, 'U' => 21, 'V' => 22, 'W' => 23, 'X' => 24, 'Y' => 25, 'Z' => 26,
            'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13,
            'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26
        );
        if (isset($pString{0})) {
            if (!isset($pString{1})) {
                $_indexCache[$pString] = $_columnLookup[$pString];          
                return $_indexCache[$pString];
            } elseif (!isset($pString{2})) {
                $_indexCache[$pString] = $_columnLookup[$pString{0}] * 26 + $_columnLookup[$pString{1}];        
                return $_indexCache[$pString];
            } elseif (!isset($pString{3})) {
                $_indexCache[$pString] = $_columnLookup[$pString{0}] * 676 + $_columnLookup[$pString{1}] * 26 + $_columnLookup[$pString{2}];
                return $_indexCache[$pString];
            }
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章