cakephp excel/csv 导出组件 [英] cakephp excel/csv export component

查看:22
本文介绍了cakephp excel/csv 导出组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to integrate Excel/CSV Export option with my CakePHP website, any idea about available components or helpers?

谢谢!

推荐答案

此解决方案适用于 CakePHP 2.0.. 你也可以将其集成到 CakePHP 1.3 中

This solution is for CakePHP 2.0.. u can also integrate it in CakePHP 1.3

步骤 1:将以下文件另存为 Csv.php 到您的 app/View/Helper 目录

Step 1: Save the following file as Csv.php into your app/View/Helper directory

<?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 添加到您的控制器:

Step 2: Add this Helper to your controller:

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

第 3 步:在控制器上创建一个方法下载",例如.home_controller.php

Step 3: create a method "download" at controller for eg. 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 的页面上

Step 4: put this link on page from where you have to download CSV

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

步骤:5(最后一步)

将此代码放在 View/Homes/download.ctp 上

Put this code on 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);
?>

这篇关于cakephp excel/csv 导出组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆