CodeIgniter Forums

Full Version: trying to use phpSpreadsheet
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I am trying to allow a user to upload and import the data from an excel file into my mysql table. To do this I am trying to use phpSpreadsheet.

I installed phpSpreadsheet with Composer. I then put their sample function into my controller in one of the functions. I can upload the file fine using the CI upload lib. The require statemenrt works good ( at least no errors!). The use statements don't work at all so I commented them out. The new Spreadsheet statement complains that


A PHP Error was encountered
Severity: Error
Message: Class 'Spreadsheet' not found
Filename: controllers/Configure.php


How can I get this to work? 
 
require 'vendor/autoload.php';

//use PhpOffice\PhpSpreadsheet\Spreadsheet;
//use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
The error is probably because you have commented out the use statements.
Hey Dave. Yes, but when I uncomment them like this,

   require 'vendor/autoload.php';
   use PhpOffice\PhpSpreadsheet\Spreadsheet;
   use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

I get this error:


Severity: Parsing Error
Message: syntax error, unexpected 'use' (T_USE)
Filename: controllers/Configure.php
Line Number: 34
That error usually means the the 'use' statement is not declared in the outermost scope of a file. In other words, at the top of the file usually right after the opening <?php
The path for "require" is incomplete.
If you installed PHPSpreadsheet in the application/third_party folder, in it's own folder phpspreadsheet, then the require should be like this:

PHP Code:
require APPPATH '/third_party/phpspreadsheet/vendor/autoload.php'
(02-07-2018, 11:09 PM)Wouter60 Wrote: [ -> ]The path for "require" is incomplete.
If you installed PHPSpreadsheet in the application/third_party folder, in it's own folder phpspreadsheet, then the require should be like this:

PHP Code:
require APPPATH '/third_party/phpspreadsheet/vendor/autoload.php'
(02-07-2018, 11:09 PM)Wouter60 Wrote: [ -> ]The path for "require" is incomplete.
If you installed PHPSpreadsheet in the application/third_party folder, in it's own folder phpspreadsheet, then the require should be like this:

PHP Code:
require APPPATH '/third_party/phpspreadsheet/vendor/autoload.php'

He used composer. autoload.php and phpoffice files are in vendor
"Require" and "Use" shouldn't be used inside a function, but before the class definition, at the top of the controller.
(02-08-2018, 11:31 AM)Wouter60 Wrote: [ -> ]"Require" and "Use" shouldn't be used inside a function, but before the class definition, at the top of the controller.

That is true of "use" but not of "require".

Not germane here, but important to know that (per the PHP manual) "A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword."
(02-08-2018, 12:00 PM)dave friend Wrote: [ -> ]
(02-08-2018, 11:31 AM)Wouter60 Wrote: [ -> ]"Require" and "Use" shouldn't be used inside a function, but before the class definition, at the top of the controller.

That is true of "use" but not of "require".

Not germane here, but important to know that (per the PHP manual) "A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword."

Right, that's why in this case, the require statement must be used at the top of the controller. The namespace can't be used if the third party php file wasn't included with require or require_once.
Pages: 1 2