2009-07-16 65 views
15

我試圖導出一個數據庫表作爲從瀏覽器下載的.csv文件。我的代碼是基於Zend框架和我幾乎沒有與下面的操作:在zend框架中導出csv

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字符串發送到瀏覽器?

BTW:我使用的是動作堆棧插件來幫助我使頭和側邊欄如下:

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

歡呼聲中,阿德里安

+0

您使用的是動作堆棧插件爲你的頭,側邊欄和頁腳? – 2009-07-16 10:33:50

回答

7

我們找到了一個解決問題的辦法。我取代了以下行

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

通過

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

如果setNeverRender()時,沒有視圖呈現(從插件兩者都不是)。

0
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; 
}` 
6

您可以使用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'); 
    ........................ 
    ........................ 
} 
1
$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;