问题
I have started to export csv files with the help of CsvView plugin, but am stuck at one place.
So here is the controller code:
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'));
}
The problem is that the whole array is printed in a single cell, where I would like every item of an array to be placed in its own cell.
Any help is much appreciated.
回答1:
this seems working for me :-
$result contains :-
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'));
来源:https://stackoverflow.com/questions/24570123/cakephp-placing-array-items-into-diferrent-cells-csvview-plugin