PHPExcel(1) 匯出功能

來源:互聯網
上載者:User
PHPExcel匯出功能

Web開發的時候,經常會遇到需要將資料庫中某些表單匯出到excel檔案中的問題。我在運用yii架構做web開發過程中,發現有個應用為PHPExcel,該應用能夠實現excel檔案的匯入匯出,剛好能夠滿足需要的要求。一下是自己應用 PHPExcel 做資料匯出的記錄。

1、首先下載該應用,github連結為:https://github.com/PHPOffice/PHPExcel

2、在 protected/extensions 路徑下建立 PHPexcel 目錄,將下載的 PHPExcel 檔案解壓。

3、將解壓後的classes目錄下所有內容,拷貝放到protected/extensions/PHPexcel 目錄下

4、在控制器檔案 XXX.Controller.php 中,先引入相關檔案
  

5、編寫方法Download(以下是自己開發過程中的部分代碼)
public function actionDownload($option)    {        // Create new PHPExcel object        $objPHPExcel = new PHPExcel();        // Set properties        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")            ->setLastModifiedBy("Maarten Balliauw")            ->setTitle("Office 2007 XLSX Test Document")            ->setSubject("Office 2007 XLSX Test Document")            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")            ->setKeywords("office 2007 openxml php")            ->setCategory("Test result file");        //$objPHPExcel->getActiveSheet()->mergeCells('A1:G1');        //$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);        //$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);        //$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);        //$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);        $subjects = Subject::model()->findAll("headteacher=:name", array(":name" => Yii::app()->session['name'])); //找出相關課程組        foreach ($subjects as $item1) {            $criteria = new CDbCriteria; // 建立CDbCriteria對象            $criteria->addCondition("suid = :id");            $criteria->params[':id'] = $item1->id;            $criteria->select = '*';            //按照返回參數搜尋選題資訊            if ($option == 1) {                $criteria->order = 'Cid';                //$criteria -> limit = 3;                $b = Selectcourse::model()->findAll($criteria);                $objPHPExcel->setActiveSheetIndex(0)                    ->setCellValue('A1', '選題情況')                    ->setCellValue('A2', '學生ID')                    ->setCellValue('B2', '學生姓名')                    ->setCellValue('C2', '課程名稱')                    ->setCellValue('D2', '題目名稱')                    ->setCellValue('E2', '選題時間');                $count = 2;                foreach ($b as $item2) {                    $count += 1;                    $l1 = "A" . "$count";                    $l2 = "B" . "$count";                    $l3 = "C" . "$count";                    $l4 = "D" . "$count";                    $l5 = "E" . "$count";                    $objPHPExcel->setActiveSheetIndex(0)                        ->setCellValue($l1, $item2->sid)                        ->setCellValue($l2, Student::model()->find(array('condition' => 'id=' . $item2->sid,))->name)                        ->setCellValue($l3, $item1->name)                        ->setCellValue($l4, Course::model()->find(array('condition' => 'Cid=' . $item2->cid,))->Cname)                        ->setCellValue($l5, $item2->apply_time);                }            }            if ($option == 2) {                $criteria->order = 'sid';                //$criteria -> limit = 3;                $b = Selectcourse::model()->findAll($criteria);                $objPHPExcel->setActiveSheetIndex(0)                    ->setCellValue('A1', '選題情況')                    ->setCellValue('A2', '學生ID')                    ->setCellValue('B2', '學生姓名')                    ->setCellValue('C2', '課程名稱')                    ->setCellValue('D2', '題目名稱')                    ->setCellValue('E2', '選題時間');                $count = 2;                foreach ($b as $item2) {                    $count += 1;                    $l1 = "A" . "$count";                    $l2 = "B" . "$count";                    $l3 = "C" . "$count";                    $l4 = "D" . "$count";                    $l5 = "E" . "$count";                    $objPHPExcel->setActiveSheetIndex(0)                        ->setCellValue($l1, $item2->sid)                        ->setCellValue($l2, Student::model()->find(array('condition' => 'id=' . $item2->sid,))->name)                        ->setCellValue($l3, $item1->name)                        ->setCellValue($l4, Course::model()->find(array('condition' => 'Cid=' . $item2->cid,))->Cname)                        ->setCellValue($l5, $item2->apply_time);                }            }        }        // Rename sheet        $objPHPExcel->getActiveSheet()->setTitle('學生選題資訊');        // Set active sheet index to the first sheet, so Excel opens this as the first sheet        $objPHPExcel->setActiveSheetIndex(0);        // Redirect output to a client’s web browser (Excel5)        header('Content-Type: application/vnd.ms-excel');        header('Content-Disposition: attachment;filename="學生選題資訊.xls"');        header('Cache-Control: max-age=0');        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');        $objWriter->save('php://output');    }



6、不要忘了在使用者存取控制裡面添加動作download
public function accessRules(){array('allow', // allow admin user to perform 'admin' and 'delete' actions                'actions' => array('download'),                'roles' => array('...'),            ),}



7、在對應的view檔案中,編寫連結
 $option)); ?>



8、完成將資料以excel檔案匯出。


以上是自己對PHPExcel的一個應用,因為是初次接觸,用的也不熟練,難免有一些不當,望大家指出,共同進步!
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.