Skip to content

yiqing-95/YiiCsvExporter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 

Repository files navigation

this exension is based on :http:https://www.yiiframework.com/extension/csvexport

just for the CActiveDataProvider ,if you use the CSqlDataProvider please do not use this
may be it will be added in next version

usage example :

first you should use this widget in your admin.php (nomorlly created by gii/giix 's crud action)


<?php
$this->widget('ext.CSVExport.CsvExporter',array(
    'cmdLable'=>'export as csv file',
   'model'=>$model
));
?>

<?php
$this->widget('zii.widgets.grid.CGridView', array(
//$this->widget('LGridView', array(
    'id' => 'user-profile-grid',
    'dataProvider' => $model->search(),
    'filter' => $model,
    'columns' => array(
        array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            // 'value'=>'$data->getPrimaryKey()',
        ),
        'uid',
        'realname',
        'birthday',
        'constellation',
        'zodiac',
        'tel',
        array(
            "header" => "Options",
            "type" => "raw",
            'value' => 'CHtml::link("link","#",array("onclick"=>"alert()"))',
        ),
        array(
            'name' => 'company',
            'value' => '$data->company',
            'filter' => "<input type='text' id='f1' class='ignore' name='f1'/><input type='text' id='f2' name='f2'/>",
        ),
        ..............
        array(
            'class' => 'CButtonColumn',
        ),
    ),
)); ?>


 then in your actionAdmin (UserProfileController::actionAdmin for me):

   public function actionAdmin()
      {
          $model = new UserProfile('search');
          $model->unsetAttributes();

          if (isset($_GET['UserProfile']))
              $model->attributes = $_GET['UserProfile'];

           //-------<added code here>---------------------------------------------------------------------------------------------
              Yii::import('ext.CSVExport.CsvExporter');
              CsvExporter::$fileName =  'theUserProfile.csv';
              CsvExporter::export($model);
              //   CsvExporter::export($model,'uid,realname',array('用户id','真实姓名')); //you can alse specify the column , the label for header
           //--------<added code here/>---------------------------------------------------------------------------------------------

          $this->render('admin', array(
              'model' => $model,
          ));
      }


      for more detail please refer to the source code


 ============================================================================================================================
 use the toexcel behavior  http:https://www.yiiframework.com/extension/toexcel/ is similar to above:

 in actionAdmin:

   public function actionAdmin()
     {
         $model = new UserProfile('search');
         $model->unsetAttributes();

         if (isset($_GET['UserProfile']))
             $model->attributes = $_GET['UserProfile'];

         if(isset($_GET['act'])){
              /*
             Yii::import('ext.CSVExport.CsvExporter');
             CsvExporter::$options = array(
                 'exportFull'=>false,
             );
             CsvExporter::export($model,'uid,realname',array('用户id','真实姓名'));
               */
             //Yii::import('ext.eexcelview.EExcelBehavior');
             $bh = Yii::createComponent(array(
                 'class'=>'ext.eexcelview.EExcelBehavior',
             ));
             $this->attachBehavior('excelExport',$bh);

             $this->toExcel($model->search(),
                 array(

                 ),
                 'Test File',
                 array(
                     'creator' => 'yiqing',
                 ),
                 'Excel2007' // This is the default value, so you can omit it. You can export to CSV, PDF or HTML too
             );
         }

         $this->render('admin', array(
             'model' => $model,
         ));
     }

====================================================================================================================
advance usage:

in admin.php :

  $this->widget('ext.CSVExport.CsvExporter',array(
        'cmdLable'=>'exportGridView as a file',
        'model'=>$model,
        'template'=>'please chooseType{ddl} {cmd}',
        'ddlOptions'=>array(
            'data'=>array('CSV'=>'csv','PDF'=>'PDF','Excel2007'=>'Excel2007','HTML'=>'HTML'),
        ),
    ));


in your controller :

public function actionAdminAdv()
    {
        $model = new User('search');
        $model->unsetAttributes(); // clear any default values
        if (isset($_GET['User']))
            $model->attributes = $_GET['User'];


        if (isset($_GET['act'])) {

            $bh = Yii::createComponent(array(
                'class' => 'ext.eexcelview.EExcelBehavior',
            ));
            $this->attachBehavior('excelExport', $bh);

            //refer to CGridView
            $columns = array(
                'nickname',
                'realName',

                array(
                    'name' => 'emailAsId',
                    'header' => 'email address',
                    'footer' => '',
                ),
            );

            $this->toExcel($model->search(),
                $columns,
                'userInfo',
                array(
                    'creator' => 'yiqing',
                ),
                isset($_GET['exportType']) ? $_GET['exportType'] : 'CSV' // This is the default value, so you can omit it. You can export to CSV, PDF or HTML too
            );
        }

        $this->render('adminAdv', array(
            'model' => $model,
        ));
    }

About

use to export CGridView as an CsvFile , based on http:https://www.yiiframework.com/extension/csvexport

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages