cakephp将数据导出到excel或Csv文件中

时间:2013-09-20 13:14:43

标签: php cakephp cakephp-2.0 export-to-excel cakephp-2.1

我正在开发一个Cakephp 2.x ..我正在使用this解决方案将数据导出到Excel工作表..问题是我不知道如何添加列名称或标题然后放入数据列下..

例如,这是我现在正在做的事情

$this->CSV->addRow(array_keys($line));
 foreach ($Contacts as $contact)
 {
      $line = $contact['Contact'];

       $this->CSV->addRow($line);
 }

$filename='orders';
 echo  $this->CSV->render($filename); 

我想让我的excel表格是这样的 就像我在视图页面中所做的那样..

      <table>

<tr>

    <th>Title</th>
    <th>Body</th>
    <th>Actions</th>
</tr>
<?php foreach($contacts as $contacts){?>
    <tr>

        <td> hello</td>
        <td> hello</td>
        <td> hello</td>
     </tr>
 }
    ?>

1 个答案:

答案 0 :(得分:6)

我的建议是使用PHPExcel。

这就是我所做的:

我将PHPExcel文件下载到Vendor文件夹中。

我的结构现在是:

app 
  |---Vendor
        |----- PHPExcel
                   |----PHPExcel
                   |----PHPExcel.php

由于我自己的原因,我需要对PHPExcel进行自己的分叉更改。因此,我下载了一个副本,而不是使用最新的存储库和一个包管理解决方案,如git submodules或composer。随意使用它们。

然后我在我的cakephp应用程序中编写了自己的Lib代码。

app 
  |---Lib
        |----- Print
                   |----Excel
                          |----SomeExcel.php
                          |----AnotherExcel.php

我写了两个类SomeExcelAnotherExcel因为我需要生成两个不同的excel文件。

SomeExcel里面我写了这样的话:

require_once(APP . DS . 'Vendor' . DS . 'PHPExcel' . DS . 'PHPExcel.php');

class SomeExcel {
    private $yourOwnPrivateProperty;
    private $objPHPExcel;

    public function __construct($data) {
        $this->objPHPExcel    = new PHPExcel();
        $this->objPHPExcel->writeDebugLog = true;

        $this->_createFilename();

    }

    public function create() {
        // you have to study PHPExcel yourself and write this
        $this->_setFileProperties();
        // you have to study PHPExcel and write whatever you want
        $this->_createSheets();
        $result = $this->_save();

        if ($result) {
            return $this->_getAttachmentFormat();
        } else {
            return $result;
        }
    }

    protected function _save() {
        $objWriter = PHPExcel_IOFactory::createWriter($this->objPHPExcel, 'Excel2007');
        /* I will create the folder inside the webroot outputfiles folder 
         * in case it does not exist.
         * I am not going to provide it here as it is not relevant to the question.
         * You need to write your own.
         */
        $this->_createFolder(); 
        try {
            $objWriter->save($this->outputPath . $this->filename);
        } catch (Exception $e) {
            return false;
        }
        return true;
    }

    protected function _getAttachmentFormat() {
        return array(
            $this->filename => array(
                'file' => $this->outputPath . $this->filename,
                'mimetype' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'contentId' => 'excelfile-for-something'
            )
        );
    }

AnotherExcel类很相似。

通常是触发生成Excel文件的控制器操作。但是,为了遵守FatModel-ThinController原则,我有一个模型方法来做到这一点。

所以我有一个像这样的控制器动作:

/**
 * print_to_excel method (testing only)
 *
 * @return void
 */
public function print_to_excel($id = null) {
    set_time_limit(120);
    $result = $this->SomeModel->printExcel($id);

    if (is_array($result)){
        $filename = key($result);
        $this->redirect('/outputfiles/Excel/' . $id . '/' . $filename);
    }
}

在我的SomeModel中,我有以下内容:

/**
 *
 * print excel file for said Quotation
 *
 * @param $id Quotation id
 * @throws NotExistException
 * @return boolean Return true if successful
 */
public function printExcel($id = null) {
    if (!$this->exists($id)) {
        throw new NotFoundException(__('Invalid whatever'));
    }


    $data = $this->find('first', array(
        'conditions' => array('SomeModel.id' => $id),
    ));

    // do whatever you need to get the data you want printed in the Excel

    App::uses('SomeExcel', 'Lib/Print/Excel');

    $someExcel = new SomeExcel($data);

    return $someExcel->create();
}

这是我使用Excel和CakePHP的方式

这是一种方式,但不是唯一的方法。

我也很自豪地告诉您,这是我为一家大型电信公司为自己的内部数字文书工作而编写的企业应用程序使用的确切代码。

该公司至少有10人使用它。

相关问题