php Excel 導入功能

下載excel類地址 https://pan.baidu.com/s/19MqAHUn4RyZ5HEAChyC0jg  密碼:mn58
php

本人用的thinkcmf框架 把類文件放在框架的類文件裏面,下面直接上代碼git

注:excel必定要放在框架公共類文件裏面,由於這個本人繞了很久,一直報錯。因此請你們注意。github

function exceladd($sheet=0){ 
        $file = $_FILES['excel']['tmp_name'];
        $file = iconv("utf-8", "gb2312", $file);   //轉碼 
        if(empty($file) or !file_exists($file)) { 
            die('file not exists!'); 
        } 
         vendor('PHPExcel.Classes.PHPExcel');//引用類文件
        $objRead = new \PHPExcel_Reader_Excel2007();   //實例化 創建reader對象 
        if(!$objRead->canRead($file)){ 
            $objRead = new \PHPExcel_Reader_Excel5(); 
            if(!$objRead->canRead($file)){ 
                die('No Excel!'); 
            } 
        } 
       
        $cellName = array('A', 'B', 'C', 'D'); 
       
        $obj = $objRead->load($file);  //創建excel對象 
        $currSheet = $obj->getSheet($sheet);   //獲取指定的sheet表 
        $columnH = $currSheet->getHighestColumn();   //取得最大的列號 
        $columnCnt = array_search($columnH, $cellName); 
        $rowCnt = $currSheet->getHighestRow();   //獲取總行數 
       
        $data = array(); 
        for($_row=2; $_row<=$rowCnt; $_row++){  //讀取內容 
            for($_column=0; $_column<=$columnCnt; $_column++){ 
                $cellId = $cellName[$_column].$_row; 
                    $cellValue = $currSheet->getCell($cellId)->getValue(); 
                    if($cellValue instanceof PHPExcel_RichText){   //富文本轉換字符串 
                        $cellValue = $cellValue->__toString(); 
                    } 
               
                $data[$_row][$cellName[$_column]] = $cellValue; 
            } 
        } 
       return $data;
       
       
    } 

  由於根據本人需求,excel表第一行是名稱,因此在獲取的時候,沒有獲取第一行名稱,因此在循環的時候$_row = 2 從2開始。框架

相關文章
相關標籤/搜索