[在继续阅读之前,如果我在这里重复,我要表示歉意。我已经环顾了很长一段时间,但对于如何处理以下情况却丝毫没有运气。由于我是新手程序员,整个事情变得越来越糟。
情况
我从工作中的高级程序员那里获得了一项任务,以创建一个可以生成CSV文件的函数。我遇到了一些麻烦,但终于解决了。该函数(位于模型中)如下所示。
// The function below will return a CSV file.
public function convert2CSV(){
$data = $this->search()->rawData;
$fileOpener = fopen('php://output', 'w');
fputcsv($fileOpener, array(
'some code...'
), ';');
foreach ($data as $rows) {
fputcsv($fileOpener, $rows ';');
}
fclose($fileOpener);
}
到目前为止,一切都很好。现在,我得到了创建一个过滤器的任务,该过滤器也将应用于CSV文件。这是什么意思?好吧,我们在网站上使用过滤器,然后导出CSV文件,它应该只下载过滤器的结果。到现在为止,无论我做什么,它都只是下载了所有可能的最大数量的文件。
我已经尝试通过使用$ _GET完成上述工作,以某种方式告诉上面的CSV函数仅下载过滤后的结果。我一直在尝试在控制器中的调用模型的CSV函数的动作中做到这一点。
public function actionExport2CSV() {
$a = new A();
$b = new B();
// This part will output the headers so that the file is downloaded rather than displayed
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=TestFile.csv');
// do not cache the file
header('Pragma: no-cache');
header('Expires: 0');
$a->convert2CSV(); {
if (isset($_GET['B'])) {
$b->attributes = $_GET['B'];
}
}
}
}
我感谢您能给我任何帮助和/或解释。我当然更愿意了解自己在做错什么,以便学习更多并将来成为更好的程序员。
非常感谢您的帮助人员。
亲切的问候。
我使用EExcelview扩展名,非常有用。
/protected/extensions/
中的代码/protected/config/main.php
文件中> 'import' => array(
'app.models.*',
'app.portlets.*',
'app.components.*',
'app.extensions.*',
...
'ext.phpexcel.*', # here we are importing the extension
),
$this->widget('EExcelView', array(
'dataProvider' => $model->search(),
'creator' => Yii::app()->name,
'grid_mode' => 'grid',
'title' => 'CSV File title',
'subject' => 'CSV File Subject',
'description' => 'CSV File Description',
'exportText' => 'Download file ',
'filename' => urlencode('My file to download'),
'exportType' => 'CSV', # Available Options => Excel5, Excel2007, PDF, HTML, CSV
'template' => "{exportbuttons}",
'columns' => array(
'colA',
'colB'
...
),
));
没关系,大家。