【问题标题】:cakephp excel/csv export componentcakephp excel/csv 导出组件
【发布时间】:2011-07-14 23:59:47
【问题描述】:

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

谢谢!

【问题讨论】:

    标签: excel cakephp csv export


    【解决方案1】:

    此解决方案适用于 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 步:将此 Helper 添加到您的控制器:

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

    第 3 步:在控制器上创建一个“下载”方法,例如。 home_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);
    ?>
    

    【讨论】:

    • 您好,在 localhost 中一切正常,但如果我在网上尝试,我无法获得下载弹出窗口,但数据会在浏览器本身中得到回显。请给我建议任何解决方案。
    • 在导出的 excel 文件中也获取 HTML 代码。然后在该代码下方我有我想要的数据列表。任何帮助为什么它的导出代码也在文件中??
    • 我使用此代码但只下载表格数据。但我需要表头和数据
    【解决方案2】:

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

    【讨论】:

    • 我们不能同时导入两者并仅选择一些选项,例如您想要 CSV 还是 Excel,而不是两个单独的帮助程序?
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      不需要任何组件或助手

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

      路由.php

      Router::parseExtensions('csv');
      

      导出.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;
      ?>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      • 1970-01-01
      • 2015-02-08
      • 2013-12-12
      • 1970-01-01
      • 2018-01-26
      • 1970-01-01
      相关资源
      最近更新 更多