将数组输出到 csv


outputting array to a csv

我正在尝试将数据从数组输出到 csv 并下载它。

我正在使用基于zend框架构建的社交引擎。

public function indexAction()
    {
        $this->outputCSV();
        //$this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')->getNavigation('passport_admin_main', array(), 'passport_admin_main_outofarea');
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        header('Content-Disposition: attachment; filename="OutOfAreaReport.csv"');
        //content type
        header('Content-type: application/excel');
        //read from server and write to buffer
        readfile('spreadsheet/OutOfArea.csv');
    }
    public function outputCSV(){
        $list = array (
                    array('aaa', 'bbb', 'ccc', 'dddd'),
                    array('123', '456', '789'),
                    array('"aaa"', '"bbb"')
                );
        $fp = fopen('OutOfAreaReport.csv', 'w');
        foreach ($list as $fields) {
            fputcsv($fp, $fields);
        }
        fclose($fp);
    }
}

目前它下载 CSV,但它在 CSV 中没有值,它是空的。

您正在使用两种不同的路径:

    readfile('spreadsheet/OutOfArea.csv');
              ^^^^^^^^^^^^
    $fp = fopen('OutOfAreaReport.csv', 'w');
                 ^----no path