cakephp excel / csv导出组件

我想将Excel / CSV导出选项与我的CakePHP网站集成,有关可用组件或助手的任何想法?

谢谢 !

这个解决scheme适用于CakePHP 2.0。您也可以将它整合到CakePHP 1.3中

第1步:将以下文件保存为Csv.php到你的app / View / Helper目录

<?php class CsvHelper extends AppHelper { var $delimiter = ','; var $enclosure = '"'; var $filename = 'Export.csv'; var $line = array(); var $buffer; function CsvHelper() { $this->clear(); } function clear() { $this->line = array(); $this->buffer = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+'); } function addField($value) { $this->line[] = $value; } function endRow() { $this->addRow($this->line); $this->line = array(); } function addRow($row) { fputcsv($this->buffer, $row, $this->delimiter, $this->enclosure); } function renderHeaders() { header('Content-Type: text/csv'); header("Content-type:application/vnd.ms-excel"); header("Content-disposition:attachment;filename=".$this->filename); } function setFilename($filename) { $this->filename = $filename; if (strtolower(substr($this->filename, -4)) != '.csv') { $this->filename .= '.csv'; } } function render($outputHeaders = true, $to_encoding = null, $from_encoding ="auto") { if ($outputHeaders) { if (is_string($outputHeaders)) { $this->setFilename($outputHeaders); } $this->renderHeaders(); } rewind($this->buffer); $output = stream_get_contents($this->buffer); if ($to_encoding) { $output = mb_convert_encoding($output, $to_encoding, $from_encoding); } return $this->output($output); } } ?> 

第2步:将这个助手添加到您的控制器:

 var $helpers = array('Html', 'Form','Csv'); 

第3步:在控制器中创build一个“下载”方法,例如: homes_controller.php

 <?php function download() { $this->set('orders', $this->Order->find('all')); $this->layout = null; $this->autoLayout = false; Configure::write('debug', '0'); } ?> 

第4步:把这个链接从你必须下载CSV的页面

 <?php echo $this->Html->link('Download',array('controller'=>'homes','action'=>'download'), array('target'=>'_blank')); ?> 

步骤:5(最后一步)

把这个代码放在View / Homes / download.ctp上

 <?php $line= $orders[0]['Order']; $this->CSV->addRow(array_keys($line)); foreach ($orders as $order) { $line = $order['Order']; $this->CSV->addRow($line); } $filename='orders'; echo $this->CSV->render($filename); ?> 

最近,我在一些需要以XLS和CSV格式导出数据的项目中工作过。 这些助手工作非常好:

http://bakery.cakephp.org/articles/ifunk/2007/09/10/csv-helper-php5

http://bakery.cakephp.org/articles/wasenbr/2007/04/12/excel-xls-helper

在cakePHP中有关于使用PHPExcel 的面包店里有一篇文章。 该库提供了以多种不同格式编写电子表格数据的选项。 在链接的面包店文章中给出的例子是xls和xlsx文件,但csv也是一个选项。

不需要任何组件或助手

大教程http://andy-carter.com/blog/exporting-data-to-a-downloadable-csv-file-with-cakephp

调节器

 public function export() { $this->response->download("export.csv"); $data = $this->Subscriber->find('all'); $this->set(compact('data')); $this->layout = 'ajax'; return; } 

route.php

 Router::parseExtensions('csv'); 

export.ctp文件

 <?php app/Views/Subscribers/export.ctp foreach ($data as $row): foreach ($row['Subscriber'] as &$cell): // Escape double quotation marks $cell = '"' . preg_replace('/"/','""',$cell) . '"'; endforeach; echo implode(',', $row['Subscriber']) . "\n"; endforeach; ?>