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

yipeiwu_com5年前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 实用代码收集

1. 可阅读随机字符串 此代码将创建一个可阅读的字符串,使其更接近词典中的单词,实用且具有密码验证功能。 复制代码 代码如下: /************** *@length - le...

PHP将DateTime对象转化为友好时间显示的实现代码

复制代码 代码如下: /** * 友好日期时间 * * @param DateTime $datetime 日期时间 * @param int $size 精确到位数 * @throws...

总结的一些PHP开发中的tips(必看篇)

一、开发习惯和php代码 1、准确的理解各种概念。现在的新东西层出不穷,望文生义和一知半解对开发工作有害无益;//比如我就碰到有人理解松散耦合(这个东西不新)的概念居然是要求代码不要有空...

php 删除无限级目录与文件代码共享

<? //删除目录 class del_path { function wm_chief_delpath($del_path) { if(!file_exists($del_pat...

学习php设计模式 php实现装饰器模式(decorator)

学习php设计模式 php实现装饰器模式(decorator)

动态的给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活【GOF95】 装饰模式是以对客户透明的方式动态地给一个对象附加上更多的职责。这也就是说,客...