在zend框架中导出csv [英] export csv in zend framework

查看:139
本文介绍了在zend框架中导出csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从浏览器导出数据库表作为.csv可下载。我的代码是基于zend框架,我几乎有以下操作:

  public function exportTableAction()
{
$ this-> _helper-> layout-> disableLayout();
$ this-> _helper-> viewRenderer-> setNoRender();

$ fileName = $ this-> _getParam('fileName');
$ tableName = $ this-> _getParam('tableName');

header('Content-type:application / octet-stream');
header('Content-Disposition:attachment; filename ='。$ fileName。'');

echo $ this-> getCsv($ tableName,$ fileName);
}

我可以下载包含有效数据的.csv文件。然而,即使我禁用了布局和渲染器,我仍然得到我的页面的头,边栏和页脚的输出在我的.csv文件的结尾。有没有办法禁用任何html输出,而不是在我的exportTableAction生成的?或者我可以用不同的方式发送头信息和csv字符串到浏览器?



BTW:我使用动作栈插件来帮助我渲染头和边栏如下:

  ... 
$ actionStack = $ front-> getPlugin('Zend_Controller_Plugin_ActionStack') ;
$ actionStack-> pushStack($ userlogAction);
$ actionStack-> pushStack($ rightcolAction);

干杯,Adrian

解决方案

我们找到了解决问题的方法。我替换了以下行

  $ this-> _helper-> viewRenderer-> setNoRender 

  $ this-> _helper-> viewRenderer-> setNeverRender(); 

如果使用setNeverRender(),则不会显示任何视图。

I'm trying to export a database table as a .csv downloadable from the browser. My code is zend framework based and I'm almost there with the following action:

public function exportTableAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();

    $fileName = $this->_getParam('fileName');
    $tableName = $this->_getParam('tableName');       

    header('Content-type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.$fileName.'"');

    echo $this->getCsv($tableName, $fileName);
}

I can download my .csv file containing valid data. However, even if I disabled the layout and the renderer, I still get the output of the header, sidebar, and footer of my page at the end of my .csv file. Is there a way to disable any html output other than the one generated in my exportTableAction? Or can I send the header information and the csv string to the browser in a different way?

BTW: I'm using the action stack plugin to help me render the header and sidebar as follows:

...
$actionStack = $front->getPlugin('Zend_Controller_Plugin_ActionStack');
$actionStack->pushStack($userlogAction);
$actionStack->pushStack($rightcolAction);

Cheers, Adrian

解决方案

We found a solution to the problem. I replaced the following line

$this->_helper->viewRenderer->setNoRender();

by

$this->_helper->viewRenderer->setNeverRender();

If setNeverRender() is used, no views are rendered (from plugin neither).

这篇关于在zend框架中导出csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆