我使用fputcsv来创建csv,但是当我在excel中打开它时,它输出的数据从第三行开始。为什么会这样呢?
我想用fputcsv创建一行列标题,最好的方法是什么?
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"');
header('Content-type: application/excel');
readfile('OutOfAreaReport.csv');
}
public function outputCSV(){
$list = array (
array('aaa', 'saasasbbb', 'ccdddc', 'dddd')
);
$fp = fopen('php://output', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
}发布于 2014-07-18 02:56:45
我知道这已经被标记为已回答,但当我遇到类似的问题时,我有很多包含,所以我添加了ob_clean;来清除缓冲区,它解决了空间问题。示例:
公共函数outputCSV(){
$list =数组( array('aaa','saasasbbb','ccdddc','dddd') );
//add here
ob_clean;$fp = fopen('php://output', 'w'); foreach ($list as $fields) { fputcsv($fp, $fields); } fclose($fp); }
https://stackoverflow.com/questions/11972864
复制相似问题