PhpExcel icon indicating copy to clipboard operation
PhpExcel copied to clipboard

Simple Example in a Controller

Open michapixel opened this issue 10 years ago • 10 comments

Can you please giva a simple Example on how to use this let's say in another Controller?

That'd be nice.

Plus: can we simply exchange the PHPExcel Class with the latest one?

michapixel avatar Jan 09 '15 09:01 michapixel

Hello

simple example on how I used this:

class ImportsController extends AppController { public $components = array('PhpExcel.PhpExcel');

public function import_excel_file($file) {
    $this->PhpExcel->loadWorksheet($file['tmp_name']);

    $maxCols = 6;

    // header check
    $header = $this->PhpExcel->getTableData($maxCols);
    if ($header[0] == 'ID') {
        $this->loadModel('Product');

        while ($prod = $this->PhpExcel->getTableData($maxCols)) {
            ...
        }
}

}

Plus: yes. You can simply exchange that for the latest version.

segy avatar Jan 09 '15 10:01 segy

Excellent.

michapixel avatar Jan 09 '15 10:01 michapixel

<?php

class ExportsController extends AppController  {
   public $name = 'Exports';
   public $components = array('PhpExcel.PHPExcel');

   public function export_complex_xls($release) {
    $this->PHPExcel->createWorksheet()->setDefaultFont('Calibri', 12);
   }
}

results in "Call to a member function createWorksheet() on a non-object" but even "PhpExcel" results in the same error.

what did i do wrong?

michapixel avatar Jan 12 '15 15:01 michapixel

Just a quick guess: a typo. It should be public $components = array('PhpExcel.PhpExcel'); and you have there public $components = array('PhpExcel.PHPExcel');

segy avatar Jan 12 '15 17:01 segy

<?php
class ExportsController extends AppController  {
   public $name = 'Exports';
   public $components = array('PhpExcel.PhpExcel');

   public function export_complex_xls($release) {
      $this->PhpExcel->createWorksheet()->setDefaultFont('Calibri', 12);
   }
}

results in "Call to a member function createWorksheet() on a non-object"

michapixel avatar Jan 13 '15 07:01 michapixel

btw: i'm using cake 2.5.4

michapixel avatar Jan 13 '15 07:01 michapixel

Hi. I think you forgot to load a worksheet. Check my example code, it starts with $this->PhpExcel->loadWorksheet($file['tmp_name']);

segy avatar Jan 13 '15 10:01 segy

I'm not trying to load a file, i'm trying to write some.

michapixel avatar Jan 13 '15 10:01 michapixel

yeah. got it. sorry.

one more thing. you mentioned updating PHPExcel library. Please try to check in the createWorksheet method of the component, if you var_dump $this->_xls after call $this->_xls = new PHPExcel();, if it is the instance of PHPExcel class.

segy avatar Jan 13 '15 11:01 segy

@michapixel I just had to install this today. Here's how I got it to work. You may have missed one of the first few steps which could cause the error "Call to a member function createWorksheet() on a non-object":

  1. Put the entire plugin in app/Plugin:
app
 |
 +-- Plugin
 |    |  
 |    +-- PhpExcel    
 |         |
 |         +-- Controller  
 |         +-- Vendor
 |         +-- View
  1. Edit your app/Config/bootstrap.php file and include this:
CakePlugin::load('PhpExcel');
  1. In your controller:
class MyController extends AppController {

    // load the PhpExcel component - case is important
    public $components = array('PhpExcel.PhpExcel');

    /**
     * Open an Excel file
     */
    function openexcel() {
        $fileName = './uploads/sample.csv';
        $objPhpExcel = $this->PhpExcel->loadWorksheet($fileName);

        // debug
        var_dump($objPhpExcel);
    }

}
  1. If all is well, you should see the object dumped:
object(PhpExcelComponent)[28]
  protected '_xls' => 
    object(PHPExcel)[35]
      private '_uniqueID' => string '55de3050834e4' (length=13)
      private '_properties' => 
        object(PHPExcel_DocumentProperties)[53]
          private '_creator' => string 'Unknown Creator' (length=15)
          private '_lastModifiedBy' => string 'Unknown Creator' (length=15)
          private '_created' => int 1440624720
          private '_modified' => int 1440624720
          private '_title' => string 'Untitled Spreadsheet' (length=20)
          ...

JasonBaier avatar Aug 26 '15 21:08 JasonBaier