簡體   English   中英

cakephp將數據導出到excel或csv文件

[英]cakephp export data into excel or Csv file

我正在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); 

我要制作我的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>
 }
    ?>

我的建議是使用PHPExcel。

這是我所做的:

我將PHPExcel文件下載到Vendor文件夾中。

我的結構現在是:

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

由於我自己的原因,我需要對PHPExcel進行自己的分叉更改。 因此,我下載了一個副本,而不是使用倉庫中的最新版本以及git子模塊或composer之類的軟件包管理解決方案。 隨意使用它們。

然后,我在cakephp應用程序中編寫了自己的庫代碼。

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原理,我有一個Model方法來做到這一點。

所以我有一個控制器動作是這樣的:

/**
 * 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個人使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM