• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
trying to use phpSpreadsheet

#1
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');
proof that an old dog can learn new tricks
Reply

#2
The error is probably because you have commented out the use statements.
Reply

#3
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
proof that an old dog can learn new tricks
Reply

#4
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
Reply

#5
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'
Reply

#6
(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'
proof that an old dog can learn new tricks
Reply

#7
(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
Reply

#8
"Require" and "Use" shouldn't be used inside a function, but before the class definition, at the top of the controller.
Reply

#9
(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."
Reply

#10
(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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.