php 在 Zend 框架中导出 csv

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1136264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:11:38  来源:igfitidea点击:

export csv in zend framework

phpzend-frameworkcsv

提问by aimfeld

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:

我正在尝试将数据库表导出为可从浏览器下载的 .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);
}

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?

我可以下载包含有效数据的 .csv 文件。但是,即使我禁用了布局和渲染器,我仍然会在 .csv 文件的末尾获得页面页眉、侧边栏和页脚的输出。有没有办法禁用除我的 exportTableAction 中生成的输出之外的任何 html 输出?或者我可以以不同的方式将标题信息和 csv 字符串发送到浏览器吗?

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

干杯,阿德里安

采纳答案by aimfeld

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).

如果使用 setNeverRender() ,则不会渲染任何视图(也不是来自插件)。

回答by Dmitro

You can use contextSwitch action helper.

您可以使用 contextSwitch 动作助手。

public $contexts = array(
    'test'     => array('csv')
);

public function testAction()
{
    $filename = time() . '.csv';
    $this->_helper->contextSwitch()->addContext('csv',
            array('suffix' => 'csv',
                  'headers' => array('Content-Type' => 'application/csv',
                                     'Content-Disposition' => 'attachment; filename="'. $filename.'"')))->initContext('csv');
    ........................
    ........................
}

回答by Diego Armando

$this->_helper->viewRenderer->setNoRender();
$this->view->layout()->disableLayout();

$response = $this->getResponse();
$response->setHeader('Content-type', 'application/octet-stream');
$response->setHeader('Content-Disposition', 'attachment; filename="contatos.csv"');

echo $your_csv_content;

回答by Kumar Sekhar

public function getCsv($tableName, $fileName)
{   
    $content = new $tableName();

    $content_arr = array();     
    $content_arr = $content->fetchAll('1=1','id ASC');

    $csv_terminated = "\n";
    $csv_separator = ",";
    $csv_enclosed = '"';
    $csv_escaped = "\";        

    $schema_insert = "";

    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
    stripslashes("Email Address")) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;

    $l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
    stripslashes("Add Date")) . $csv_enclosed;
    $schema_insert .= $l;
    $schema_insert .= $csv_separator;

    $out = trim(substr($schema_insert, 0, -1));
    $out .= $csv_terminated;

    if(count($content_arr) > 0)
    {               
        foreach($content_arr as $content)
        {   
             $schema_insert = '';

            if ($content->email != '' || $content->add_date != '')
            {                           
                    $schema_insert .= $csv_enclosed . 
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $content->email) . $csv_enclosed;

                     $schema_insert .= $csv_separator;

                    $schema_insert .= $csv_enclosed . 
                    str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $content->add_date) . $csv_enclosed;
                    $schema_insert .= $csv_separator;

           }        

            $schema_insert .= $csv_separator;
            $out .= $schema_insert;
            $out .= $csv_terminated;

        }
    }
    return $out;
}`