如何在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/

相关文章

简单实现限定phpmyadmin访问ip的方法

修改:config.inc.php 在第一行加入 $ip_prefix = '192.168.121.'; if (substr($_SERVER['REMOTE_ADDR'], 0,...

深入Apache与Nginx的优缺点比较详解

1、nginx相对于apache的优点:  轻量级,同样起web 服务,比apache占用更少的内存及资源  抗并发,nginx 处理请求是异步非...

PHP基于反射获取一个类中所有的方法

本文实例讲述了PHP基于反射获取一个类中所有的方法。分享给大家供大家参考,具体如下: 当我们使用一个类时既没有源码也没有文档时(尤其是php扩展提供的类,比如mysqli,Redis类)...

nginx下安装php7+php5

起步 之前在服务器搭建了lamp环境,想换用性能更强的nginx作为服务器软件,又想将php5升级为php7. 安装nginx无需赘述:sudo apt-get install ng...

php实现数组中索引关联数据转换成json对象的方法

本文实例讲述了php实现数组中索引关联数据转换成json对象的方法。分享给大家供大家参考。具体实现方法如下: public static function encode(&$var)...