yii cgridview导出到excel不工作分页

时间:2013-09-27 07:50:49

标签: excel yii pagination export-to-csv

我的代码导出到excel工作正常,如果我过滤我的代码导出过滤行作为我的条件的行,则导出1到10行。如果我点击下一页,例如11到20,然后点击导出按钮,只导出第一页1到10行。

在我的管理视图中导出按钮代码:

<div id='menub'><?php    $this->widget('zii.widgets.CMenu', array(
                   'encodeLabel'=>false,
                   'htmlOptions'=>array(
                       'class'=>'actions'),
                       'items'=>array(
                                   array(
                                       'label'=>'<img align="absmiddle" alt = "'.Yii::t('internationalization','Export'). '" src = "'.Yii::app()->request->baseUrl.'/images/export.jpg" />',
                                       //'label'=>'Export',
                                       'url'=>array('expenses/excel'),
                                    ),
                                    ),

            ));

以上链接调用费用控制器中的excel方法。 我的控制器中的代码:

    public function actionExcel() {

            $issueDataProvider = $_SESSION['report-excel'];
        $i = 0;
        $data = array();
        //fix column header. 
        //Could have used something like this - $data[]=array_keys($issueDataProvider->data[0]->attributes);. 
        //But that would return all attributes which i do not want
        //$data[]=array_keys($issueDataProvider->data[0]->attributes);
        $data[$i]['expenses_type_id'] = 'Type';
        $data[$i]['amount'] = 'Amount';
        $data[$i]['exp_date'] = 'Date';
        $data[$i]['description'] = 'Description';
        $i++;

        //populate data array with the required data elements
        foreach($issueDataProvider->data as $issue)
        {
            $data[$i]['expenses_type_id'] = $issue->expensesType->name;
            $data[$i]['amount'] = $issue['amount'];
            $data[$i]['exp_date'] = $issue['exp_date'];
            $data[$i]['description'] = $issue['description'];
            $i++;
        }
Yii::import('application.extensions.phpexcel.JPhpExcel');
        $xls = new JPhpExcel('UTF-8', false, 'test');
        $xls->addArray($data);
        $xls->generateXML('test_file');
}

我在

中保存数据
  

$ _ SESSION [ '报告-的excel']

并在我的模型中:

public function getSearchCriteria()
        {
    $criteria=new CDbCriteria;

        if(!empty($this->from_date) && empty($this->to_date))
        {
            $criteria->condition = "exp_date >= '$this->from_date'";  // date is database date column field
        }elseif(!empty($this->to_date) && empty($this->from_date))
        {
            $criteria->condition = "exp_date <= '$this->to_date'";
        }elseif(!empty($this->to_date) && !empty($this->from_date))
        {
            $criteria->condition = "exp_date  >= '$this->from_date' and exp_date <= '$this->to_date'";
        }

        $criteria->with = 'expensesType';
        $criteria->join = 'LEFT JOIN expenses_type p ON t.expenses_type_id = p.id'; 
        //s$criteria->compare('id',$this->id,true);
        $criteria->compare('p.name',$this->expenses_type_id,true);
        $criteria->compare('amount',$this->amount,true);
        $criteria->compare('exp_date',$this->exp_date,true);
        $criteria->compare('description',$this->description,true);
        $criteria->order ='exp_date DESC';

    return $criteria;
    }


    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.
$data = new CActiveDataProvider(get_class($this), array(
                        'pagination'=>array('pageSize'=> Yii::app()->user->getState('pageSize',                                           
                                Yii::app()->params['defaultPageSize']),),
                                'criteria'=>$this->getSearchCriteria(),


    ));

                     $_SESSION['report-excel']=$data;
         return new CActiveDataProvider(get_class($this), array(
        'criteria'=>$this->getSearchCriteria(),
    ));

每件事情都很好但是在分页上。 请帮助。

1 个答案:

答案 0 :(得分:0)

CActiveDataProvider包含一组项目(全部)。因此,当您调用actionExcel()时,$ page参数将丢失。因此,当您在控制器中执行actionAdmin()或在模型中执行search()时,将$ _GET ['page']保存到其他会话值。然后在执行actionExcel()时设置它。

$_GET['page'] = $_SESSION['your_session_page_value']; 

希望这有帮助

最好的问候