Symfony2框架中PHPExcel的用法

    說到PHPExcl,確實是一個功能強大的php插件,在網上一收一大把相關教程。最近在Symfony2中用到它,感覺內容還是挺多的;而且Symfony2將此插件做了二次封裝,想用它,確實得做些工作。我在網上找了很久,沒發現有Symfony2框架的PHPExcl的用法,苦老了,這框架裏又不能直接用(Symfony2框架有嚴格的流程控制);最後找到一國外網站,專門有講這款插件針對Symfony2框架的用法,參考了下,方法都被封裝好了,很規矩,開發很快。下面我就來介紹一下這個插件的用法。

    


   

    做這個之前,你首先要創建好項目(創建好一個Bundle),相關教程可以參考:

    PHPExcel分爲對Excel的導出和導入兩種方法,我是參考此版本的Excel來講的 https://github.com/liuggio/ExcelBundle,大家可以打開這個網站結合我的講解來做。下面繼續講:

    安裝(composer):首先得安裝此插件,也就是composer的安裝,可以收下composer的安裝方法。方法如下(你也可以直接下載,拷貝文件夾到項目,最後需要配一下Symfony2,使他支持此插件):

    "require" : {
        "friendsofsymfony/user-bundle": "2.0.*@dev",
    }
 //實際在win命令行下輸入下面代碼即可:
 $ php composer.phar require friendsofsymfony/user-bundle:2.0.*@dev

       安裝(phpexcelbundle):

    1 安裝require

    "require" : {
        "liuggio/excelbundle": "~2.0",
    }


 //實際在win命令行下輸入下面代碼即可:
 $ php composer.phar require liuggio/excelbundle:~2.0

    2 配置 app/AppKernel.php

    $bundles = array(        
        // ...        
        new Liuggio\ExcelBundle\LiuggioExcelBundle(),    
    );


    PS:詳細的安裝第三方Bundle可以參考:http://9670708.blog.51cto.com/9660708/1591009 



    導出:導出方法很簡單,在https://github.com/liuggio/ExcelBundle這裏也有簡單的介紹到,上面的步驟完成後,按照這個例子,你應該在你的(實際上就是將下面的內容拷貝到你寫的任何一個model中)Symfony\Bundle\FrameworkBundle\Controller\DefaultController.php文件中直接複製以下代碼即可使用(其他導入方法,可以在上面網站中尋找)

namespace YOURNAME\YOURBUNDLE\Controller;    
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller{    

     public function indexAction(){       
         //創建一個空Excel對象
         $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();   
         //設置文件屬性   
         $phpExcelObject->getProperties()
             ->setCreator("liuggio")
             ->setLastModifiedBy("Giulio De Donato")
             ->setTitle("Office 2005 XLSX Test Document")
             ->setSubject("Office 2005 XLSX Test Document")
             ->setDescription("Test document for Office 2005 XLSX, generated using PHP classes.")      
             ->setKeywords("office 2005 openxml php")
             ->setCategory("Test result file"); 
         //設置文件內容
         $phpExcelObject->setActiveSheetIndex(0)
             ->setCellValue('A1', 'Hello')
             ->setCellValue('B2', 'world!');
         //設置當前表的標題   
         $phpExcelObject->getActiveSheet()
             ->setTitle('Simple');         
         $phpExcelObject->setActiveSheetIndex(0);
         //創建一個寫對象    
         $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel5');      
         $response = $this->get('phpexcel')->createStreamedResponse($writer);
         //設置HTTP協議(創建下載文件)   
         $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');    
         $response->headers->set('Content-Disposition', 'attachment;filename=stream-file.xls');        
         $response->headers->set('Pragma', 'public');        
         $response->headers->set('Cache-Control', 'maxage=1');        
         return $response;            
    }
}

    


    

    導入:(導入就比較複雜了,但也不困難,只是需要設置的比較多),將一下函數複製,更改下文件名即可

public function import(){        
        
        $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($fliename);
        //$inputFileType = 'Excel2007';//這個是計xlsx的
        //指定爲哪張表(默認第一張表)
        //$objWorksheet = $phpExcelObject->getActiveSheet("sheet1");
        $objWorksheet = $phpExcelObject->getActiveSheet();
        //獲取行數
        $highestRow = $objWorksheet->getHighestRow();
        $highestColumn = $objWorksheet->getHighestColumn();
        //獲取列數
        $highestColumnIndex = 12;
        //遞歸輸出Excel內容
        $headtitle=array();
        for ($row = 1;$row <= $highestRow;$row++)
        {
            $strs=array();
            for ($col = 0;$col < $highestColumnIndex;$col++)
            {
                $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
            }
           $headtitle[$row]=$strs;
        }
        return $headtitle;
}

    

    這樣就可以導入excel 文件了,但是在有多張excel時會有問題,會找不到第二張表,所以這個時候得做些處理:$objWorksheet = $phpExcelObject->getActiveSheet("??");//裏面加入參數即可!

 

    更多phpexcel的方法可以在你的安裝文件裏找到(方法和例子)

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