首页 > 代码库 > Yii框架中导出excel文件

Yii框架中导出excel文件

 1 <?php
 2 
 3 namespace frontend\models;
 4 //include $_SERVER[‘DOCUMENT_ROOT‘].‘/PHPExcel/EXCEL/PHPExcel.php‘;
 5 // include ‘E:\www\WWW\advanced\common\widgets\PHPExcel\EXCEL\PHPExcel\Writer\Excel2007.php‘;
 6 use yii\base\Model;
 7 
 8 /**
 9  * This is the model class for table "country".
10  *
11  * @property string $code
12  * @property string $name
13  * @property integer $population
14  */
15 class PHPExcel extends Model
16 {
17 
18     //文件名
19     private $fileName = ‘user‘;
20 
21     //构造函数
22     public function __construct($fileName = ‘‘)
23     {
24         $this->setFileName($fileName);
25     }
26 
27     //设置要导出的文件名
28     public function setFileName($fileName)
29     {
30         $this->fileName = $fileName;
31     }
32 
33     //开始下载
34     public function toDownload($strTable)
35     {
36         header("Content-type: application/vnd.ms-excel");
37         header("Content-Type: application/force-download");
38         header("Content-Disposition: attachment; filename=".$this->fileName."_".date(‘Y-m-d‘).".xls");
39         header(‘Expires:0‘);
40         header(‘Pragma:public‘);
41         echo ‘<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />‘.$strTable.‘</html>‘;
42     }
43 
44 }
 1 //点击生成单页数据的excel表格
 2     public function actionExcel()
 3     {
 4         $id = Yii::$app -> request ->get(‘id‘);
 5         $sql="select * from order_ticket where id IN ($id)";
 6         $result = \yii::$app->db->createCommand($sql)->queryAll();
 7         $strTable =‘<table width="500" border="1">‘;
 8         $strTable .= ‘<tr>‘;
 9         $strTable .= ‘<td style="text-align:center;font-size:12px;" width="150px">姓名</td>‘;
10         $strTable .= ‘<td style="text-align:center;font-size:12px;" width="*">手机</td>‘;
11         $strTable .= ‘<td style="text-align:center;font-size:12px;" width="*">开始时间</td>‘;
12         $strTable .= ‘<td style="text-align:center;font-size:12px;" width="*">结束时间</td>‘;
13         $strTable .= ‘<td style="text-align:center;font-size:12px;" width="*">电影名称</td>‘;
14         $strTable .= ‘<td style="text-align:center;font-size:12px;" width="*">座位</td>‘;
15         $strTable .= ‘</tr>‘;
16 
17         foreach($result as $key=>$val)
18         {
19             $strTable .= ‘<tr>‘;
20             $strTable .= ‘<td style="text-align:center;font-size:12px;">‘.$val[‘reserve_name‘].‘ </td>‘;
21             $strTable .= ‘<td style="text-align:center;font-size:12px;">‘.$val[‘tel‘].‘ </td>‘;
22             $strTable .= ‘<td style="text-align:center;font-size:12px;">‘.$val[‘begin_time‘].‘</td>‘;
23             $strTable .= ‘<td style="text-align:center;font-size:12px;">‘.$val[‘end_time‘].‘ </td>‘;
24             $strTable .= ‘<td style="text-align:center;font-size:12px;">‘.$val[‘reserve_movie‘].‘ </td>‘;
25             $strTable .= ‘<td style="text-align:center;font-size:12px;">‘.$val[‘nameStr‘].‘ </td>‘;
26             $strTable .= ‘</tr>‘;
27         }
28         $strTable .=‘</table>‘;
29         $reportObj = new PHPExcel();
30         $reportObj->setFileName(‘demo‘);
31         $reportObj->toDownload($strTable);
32     }

 

 

Yii框架中导出excel文件