Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Example in a Controller #14

Open
michapixel opened this issue Jan 9, 2015 · 10 comments
Open

Simple Example in a Controller #14

michapixel opened this issue Jan 9, 2015 · 10 comments

Comments

@michapixel
Copy link

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?

@segy
Copy link
Owner

segy commented Jan 9, 2015

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.

@michapixel
Copy link
Author

Excellent.

@michapixel
Copy link
Author

<?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 michapixel reopened this Jan 12, 2015
@segy
Copy link
Owner

segy commented Jan 12, 2015

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

@michapixel
Copy link
Author

<?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
Copy link
Author

btw: i'm using cake 2.5.4

@segy
Copy link
Owner

segy commented Jan 13, 2015

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

@michapixel
Copy link
Author

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

@segy
Copy link
Owner

segy commented Jan 13, 2015

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.

@JasonBaier
Copy link

@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)
          ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants