如何在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中date与gmdate的区别及默认时区设置

一、date与gmdate有什么区别?PHP时间函数中有两个格式化函数:date()和gmdate(),在官方的文档中的描述为:复制代码 代码如下:date()  ...

PHP简单计算两个时间差的方法示例

本文实例讲述了PHP简单计算两个时间差的方法。分享给大家供大家参考,具体如下: <?php //PHP计算两个时间差的方法 $startdate="2010-12-1...

ucenter中词语过滤原理分析

本文分析了ucenter中词语过滤原理。分享给大家供大家参考,具体如下: 过滤词语表: id admin find r...

PHP大转盘中奖概率算法实例

本文实例讲述了PHP大转盘中奖概率算法的实现方法,分享给大家供大家参考。具体如下: 大转盘是最近很多线上网动中一个比较有意思的东西了,下面我们就来看看这个大转盘中奖概率算法与例子,希望对...

PHP程序中的文件锁、互斥锁、读写锁使用技巧解析

文件锁 全名叫 advisory file lock, 书中有提及。 这类锁比较常见,例如 mysql, php-fpm 启动之后都会有一个pid文件记录了进程id,这个文件就是文件锁。...