public function actionExport() { if (isset($_POST['profile_fields'])) { $fields = ''; foreach ($_POST['profile_fields'] as $field) { $fields .= $field . ','; } $fields = substr($fields, 0, -1); Yii::import('application.modules.user.components.CSVExport'); $sql = sprintf('select %s from profiles where %s', $fields, Yum::module()->customCsvExportCriteria); $result = Yii::app()->db->createCommand($sql)->queryAll(); $csv = new CSVExport($result); $content = $csv->toCSV(); $filename = Yii::app()->basePath . '/runtime/yum_user_export.csv'; $content = $csv->toCSV($filename, ",", "\""); Yii::app()->getRequest()->sendFile(basename($filename), @file_get_contents($filename), "text/csv", false); exit; } }
/** * @static * @param CActiveRecord $model * @param array|string $columns can also passed from js ,if so you can use $_GET to retrive it * array('id','name') | 'id,name' 或者带别名 't.id,user.name....' * @param array $headers */ public static function export(CActiveRecord $model, $columns = array(), $headers = array()) { if (isset($_GET[self::$actionKey])) { require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'CSVExport.php'; $provider = $model->search(); if (!empty($columns)) { $criteria = $provider->getCriteria(); $criteria->select = $columns; } $csv = new CSVExport($provider); if (!empty($headers)) { $csv->headers = $headers; } $csv->headers = $model->attributeLabels(); $csv->exportFull = false; //default use pagination if (!empty(self::$options)) { foreach (self::$options as $key => $value) { $csv->{$key} = $value; } } // echo var_export($provider->getCriteria()->toArray(),true); $content = $csv->toCSV(null, "\t", '"'); Yii::app()->getRequest()->sendFile(self::$fileName, $content, "text/csv", false); die; // exit; } /* else{ $actionKey = self::$actionKey; throw new CException("something wrog with your request,must contain the key '{$actionKey}' your get is: ".CJSON::encode($_GET) ); }*/ }
<? Yii::import('application.extensions.CSVExport'); $provider = $_SESSION['datos_filtrados']->getData(); $csv = new CSVExport($provider); $content = $csv->toCSV(); $content = $csv->toCSV('../myfilename.csv', "\t", "'"); Yii::app()->getRequest()->sendFile($filename, $content, "text/csv", false); exit(); ?>