欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-10-05 21:40:23
开始: 复制代码 代码如下: public function executeregistrantstocsv(){ $id = $this->getrequestpa...
开始:
复制代码 代码如下:

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:
if it doesn't work, try this: