我已经开始在CsvView插件的帮助下导出csv文件了,但我被困在一个地方。
所以这是控制器代码:
function export(){
$data = array(
array('37899810', '50001', '1', '7616'),
);
$_serialize = 'data';
$this->response->download('export.csv');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('data', '_serialize'));
}
问题是整个数组都打印在一个单元格中,我希望数组中的每个项目都放在自己的单元格中。
非常感谢任何帮助。
答案 0 :(得分:1)
这似乎对我有用: -
$ result包含: -
Array
(
[0] => Array
(
[Eshop] => Array
(
[name] => test shop
)
[Productlps] => Array
(
[name] => product
)
[ProductViewLog] => Array
(
[credits_left] => 4999
[ip_address] => 10.0.0.1
[created] => 2013-12-02 06:25:07
)
)
)
$excludePaths = array(); // Exclude all id fields
$_extract = $this->CsvView->prepareExtractFromFindResults($results, $excludePaths);
$customHeaders = array('ProductViewLog.created' => 'Date Accessed','Eshop.name'=>'Shop Name','Productlps.name'=>'Product Name','ProductViewLog.credits_left'=>'Credit Balance','ProductViewLog.ip_address'=>"Accessed From");
$_header = $this->CsvView->prepareHeaderFromExtract($_extract, $customHeaders);
$_serialize = 'results';
$this->response->download('my_file.csv');
$this->viewClass = 'CsvView.Csv';
$this->set(compact('results', '_serialize', '_header', '_extract'));