export csv in zend framework(在 Zend 框架中导出 csv)
问题描述
我正在尝试将数据库表导出为可从浏览器下载的 .csv.我的代码是基于 zend 框架的,我几乎可以通过以下操作实现:
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);
}
我可以下载包含有效数据的 .csv 文件.但是,即使我禁用了布局和渲染器,我仍然会在 .csv 文件的末尾获得页面的页眉、侧边栏和页脚的输出.有没有办法禁用除我的 exportTableAction 中生成的输出之外的任何 html 输出?或者我可以用不同的方式将标题信息和 csv 字符串发送到浏览器吗?
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);
干杯,阿德里安
推荐答案
我们找到了问题的解决方案.我替换了以下行
We found a solution to the problem. I replaced the following line
$this->_helper->viewRenderer->setNoRender();
由
$this->_helper->viewRenderer->setNeverRender();
如果使用 setNeverRender(),则不会渲染任何视图(也不来自插件).
If setNeverRender() is used, no views are rendered (from plugin neither).
这篇关于在 Zend 框架中导出 csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Zend 框架中导出 csv


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01