cakephp excel / csv导出组件

时间:2011-03-14 12:44:11

标签: excel cakephp csv export

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

谢谢!

4 个答案:

答案 0 :(得分:18)

此解决方案适用于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:在控制器上创建一个方法“下载”,例如。 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);
?>

答案 1 :(得分:1)

最近,我参与了一些需要在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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

无需任何组件或助手

很棒的教程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;
?>