CodeIgniter Forums
Install Library Phpspreadsheet via Composoer Autoload not working? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Install Library Phpspreadsheet via Composoer Autoload not working? (/showthread.php?tid=77624)



Install Library Phpspreadsheet via Composoer Autoload not working? - mrardiyansyah - 09-25-2020

I just installed Phpoffice/Phpspreadsheet via Composer but I can't create new instance of this library. I've checked in the composer.json file, there is already a phpspreadsheet in require.

But when I try to create a new instance, There's an error "Undefined type 'PhpOffice\PhpSpreadsheet\Reader\Xlsx'".

Please help me to find out what's wrong.


RE: Install Library Phpspreadsheet via Composoer Autoload not working? - paulbalandan - 09-25-2020

Can you show how you used it on your code? Maybe you are missing the use statement?


RE: Install Library Phpspreadsheet via Composoer Autoload not working? - mrardiyansyah - 09-25-2020

(09-25-2020, 09:12 AM)paulbalandan Wrote: Can you show how you used it on your code? Maybe you are missing the use statement?
Code:
public function readerXlsx()
{
// Create instance of Phpspreadsheet
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx(); //And this instance show an error "Undefined type ...."

}

I follow the same way as in the documentation to create instances to write a phpspreadsheet.


RE: Install Library Phpspreadsheet via Composoer Autoload not working? - John_Betong - 09-30-2020

@mrardiyansyah,

I have been using a plain PHP application and PHP Spreadsheet for quite a few years.

I have PhpSpreadsheet installed in /var/www/ci2/vendor and use the following script to create a new instance:

PHP Code:
require '/var/www/ci2/vendor/autoload.php';  
    
use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;


//==============================================
PRIVATE function _convertXlsToCsv
(
  
string $xls
  
string $csv
)
:
bool
{
  
$result FALSE;

  try {
    
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($xls);
    
$writer = new  \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet);
    
$writer->setUseBOM(true);
    
$writer->save($csv);
  }catch (
Exception $e){
    
print_r($e->message(), '$e->message()''red');
  }
  
$result file_exists($csv);

  return 
$result;
}
//