PhpExcel
PhpExcel copied to clipboard
Simple Example in a Controller
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?
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.
Excellent.
<?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?
Just a quick guess: a typo. It should be public $components = array('PhpExcel.PhpExcel'); and you have there public $components = array('PhpExcel.PHPExcel');
<?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"
btw: i'm using cake 2.5.4
Hi. I think you forgot to load a worksheet. Check my example code, it starts with $this->PhpExcel->loadWorksheet($file['tmp_name']);
I'm not trying to load a file, i'm trying to write some.
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.
@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":
- Put the entire plugin in app/Plugin:
app
|
+-- Plugin
| |
| +-- PhpExcel
| |
| +-- Controller
| +-- Vendor
| +-- View
- Edit your app/Config/bootstrap.php file and include this:
CakePlugin::load('PhpExcel');
- 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);
}
}
- 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)
...