如何在symfony中导出为CSV文件中的数据

yipeiwu_com6年前PHP代码库
开始:
复制代码 代码如下:

public function executeRegistrantsToCsv(){

$id = $this->getRequestParameter('id');

$c = new Criteria();
$c->add(RegistrantPeer::EVENT_ID, $id);
$c->add(RegistrantPeer::STATUS, 1);
$this->aObjReg = RegistrantPeer::doSelect($c);

$this->forward404Unless($this->aObjReg);
$this->setlayout('csv');

$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Type', 'application/vnd.ms-excel');
$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename=registrants_report_event_' . $id . '.csv');

}

在模板registrantsToCsvSuccess.php:
复制代码 代码如下:

Title,Name,Email,Phone,Organisation,State,City,Country,Login Date,IpAddress
<? foreach($aObjReg as $r): ?>
<?= $r->getTitle() ?>,<?= $r->getName() ?>,<?= $r->getEmail() ?>,<?= $r->getPhone() ?>,<?= $r->getOrganisation() ?>,<?= $r->getState() ?>,<?= $r->getCity() ?>,<?= $r->getCountry() ?>,<?= $r->getLoginDate() ?>,<?= $r->getIpAddress() ?>,
<? endforeach ?>

in the templates/csv.php:
<?php echo $sf_data->getRaw('sf_content') ?>
From: http://blog.baddog.net.au/sonius/steve-sonius/how-to-export-data-as-a-csv-file-in-symfony/
If it doesn't work, try this:http://blog.baddog.net.au/sonius/steve-sonius/how-to-export-data-as-an-xls-or-csv-file-from-the-admin-generator-in-symfony-1-4/

相关文章

php缩放图片(根据宽高的等比例缩放)实例介绍

推荐一个简单实用的缩放图片工具 SimpleImage,参考http://www.white-hat-web-design.co.uk/blog/resizing-images-with...

VIM中设置php自动缩进为4个空格的方法详解

代码自然少不了文本编辑利器vim,自动缩进是用制表符的,即便自己删掉制表符改成4个空格,也会在下一层次缩进中继续用制表符,这就造成了空格和制表符混用可以修改、etc/vimrc的信息:其...

php异常处理捕获错误整理

PHP的错误报告有三种: 1、错误,语法解析错误,致命错误 2、警告 3、注意 后果: 错误 -> 致命错误,会终止已下程序的执行,语法错误的话,PHP压根就没执行。 警告 -&g...

PHP循环获取GET和POST值的代码

复制代码 代码如下:if(is_array($HTTP_GET_VARS))     {     &nbs...

深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复

PHP Warning: strtotime(): It is not safe to rely on the system's timezone settings. You are *...