Yii Excel写入生成的标题没有行

时间:2014-09-09 09:53:36

标签: php excel yii

我在Yii中使用Excel写入。 每次我生成Excel时,它只给出了标题但没有输出,尽管我的数据库包含行。

控制器:

$sql = "SELECT trucker as Trucker, (SELECT COUNT(*) FROM courses where association_trucker_id = assoc_truckerid) as Units, CONCAT(buss_add_add,', ',buss_add_prov,', ',buss_add_city,', ',buss_add_zip) as Address, CONCAT(rep_lname,', ',rep_fname,' ',rep_mname) as 'Contact Person', rep_telno as 'Office No', rep_email as Email FROM `students`"; // your sql
                $rawData =  Yii::app()->db->createCommand($sql)->queryAll(); // you get them in an array
                $count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM (' . $sql . ') as count_alias')->queryScalar();
                $model = new CArrayDataProvider($posts,
                            array(
                                'totalItemCount' => $count,
                                'sort' => array(
                                    'attributes' => array(
                                        'trucker'
                                    ),
                                ),
                                'pagination' => array(
                                    'pageSize' => 5000000, // large number so you export all to excel
                                ),
                            )
                        );
                        $this->renderPartial('truckerexcel', array(
                            'model' => $model, 'truckerid' => $truckerlist,
                        ));

查看:

<?php
$this->widget('EExcelWriter', array(
    'dataProvider' => $model,
    'title' => 'EExcelWriter',
    'stream' => TRUE,
    'fileName' => 'TRUCKERS_'.date('Y-m-d', time()).'.xls',
    'columns' => array(
        'trucker'
    ),
));
?>

1 个答案:

答案 0 :(得分:0)

而不是

$model = new CArrayDataProvider($posts,

您应该使用变量$rawData

$model = new CArrayDataProvider($rawData,