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

myn*_*utt 1 php cakephp export-to-excel cakephp-2.0 cakephp-2.1

我正在使用Cakephp 2.x ..我正在使用解决方案将数据导出到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); 
Run Code Online (Sandbox Code Playgroud)

我想要制作我的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>
 }
    ?>
Run Code Online (Sandbox Code Playgroud)

Kim*_*cks 6

我的建议是使用PHPExcel.

这是我做的:

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

我的结构现在是:

app 
  |---Vendor
        |----- PHPExcel
                   |----PHPExcel
                   |----PHPExcel.php
Run Code Online (Sandbox Code Playgroud)

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

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

app 
  |---Lib
        |----- Print
                   |----Excel
                          |----SomeExcel.php
                          |----AnotherExcel.php
Run Code Online (Sandbox Code Playgroud)

我写了两个类SomeExcel,AnotherExcel因为我需要生成两个不同的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'
            )
        );
    }
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的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();
}
Run Code Online (Sandbox Code Playgroud)

这是我使用Excel和CakePHP的方式

这是一种方式,但不是唯一的方式.

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

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