yii2手動添加外掛程式PHPExcel

來源:互聯網
上載者:User

yii2手動添加外掛程式PHPExcel

yii2手動添加外掛程式PHPExcel

1.:https://github.com/PHPOffice/PHPExcel

2.解壓並修改檔案名稱為phpexcel

之後在yii項目的vendor目錄下建立一個檔案夾命名為phpoffice

把phpexcel檔案放到phpoffic檔案夾下

此時查看vendor\phpoffice\phpexcel目錄下會看到這些檔案

 3.到vendor\composer目錄下修改檔案autoload_namespaces.php,

添加一句代碼註冊命名空間

'PHPExcel' => array($vendorDir.'/phpoffice/phpexcel/Classes'),

接下來就可以開始使用了;

    //excel匯入函數    public function import($fileName){        $objReader = new \PHPExcel();             $file = $fileName;               $objPHPExcel = \PHPExcel_IOFactory::load($file);         $allSheet = $objPHPExcel->getSheetCount();        for($i=0; $i<$allSheet;$i++){            $dataSheet = $objPHPExcel->getSheet($i)->toArray('', true, true);            if(!$this->saveData($dataSheet)) return '0';        }        return '1';    } 

使用方法是直接   $objReader = new \PHPExcel();

記住執行個體化時前面要加 \  

以上只是我自己寫的例子,本教程是寫怎麼手動引入PHPExcel外掛程式的,具體用法就不作多介紹了,可自行百度;

注意執行下面步驟需備份檔案,以防萬一:

php版本大於5.6可能會報錯找不到該類;

如要支援就需要修改vendor\composer目錄下的autoload_real.php檔案

把關於$useStaticLoader這個變數的if判斷的代碼都刪了;如紅框標識的代碼都刪了; 

之後再把vendor\composer目錄下的autoload_static.php檔案刪了,就可以支援更高版本的php了; 

注意看>= 50600那部分的代碼就明白為什麼php版本大於5.6可能會報錯找不到該類;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.