2011-03-14 3 views

답변

18

이 솔루션은 유는 1 단계

CakePHP는 1.3에 통합 할 수 있습니다 .. CakePHP는 2.0입니다 : /보기/도우미 디렉토리의 앱에 Csv.php로 다음 파일을 저장

<?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 단계 :보기에이 코드를 넣어 5 (최종 단계)

/주택 : 당신이 CSV

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

단계를 다운로드해야 할 곳에서 페이지의 링크를 넣어 /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); 
?> 
+0

안녕하세요. 모든 것이 잘 작동하고 localhost가 좋지만 온라인으로 접속하려고한다면 다운로드 팝업을받을 수 없지만 브라우저 자체에 데이터가 표시됩니다. 제발 어떤 해결책을 제안 해주세요. –

+0

내 보낸 데이터를 Excel 코드로 가져 오는 중 .. 그 코드 아래에 원하는 데이터 목록이 있습니다. 어떤 도움도 왜 파일에 내보내기 코드가 ?? –

+0

cakephp 3에서 작동합니까? – Faisal

1

CakePHP를 내 PHPExcel 사용에 대한 the bakery에있는 기사가있다. 이 라이브러리는 스프레드 시트 데이터를 다양한 형식으로 작성하기위한 옵션을 제공합니다. 링크 된 제과점 기사에 제공된 예제는 xls 및 xlsx 파일 용이지만 csv도 옵션입니다.

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; 
?> 
+0

http : // andy-carter라는 원본 저자의 글을 잊어 버렸습니다.co.kr/blog/exporting-data-to-a-downloadable-csv-file-with-cakephp – rocknrollcanneverdie

관련 문제