Import class PHPEXCEL

Hello
I have a problem when I want to import an excel file into cakeph 3, here is the error message I get: Class ‘App \ Controller \ PHPExcel_IOFactory’ not found

here is the code for my controller:

use Cake\ORM\TableRegistry;
use App\Model\Table\Contrat;
use PHPExcel\Classes\PHPExcel;
use PHPExcel\Classes\PHPExcel\IOFactory;

require ROOT.DS.‘vendor’.DS.‘phpoffice/phpexcel/Classes/PHPExcel.php’;
require ROOT.DS.‘vendor’.DS.‘phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php’;

My function importContrat :
public function importContrat()
{

	if(!empty($this->request->data))
	{

		$this->loadModel('Marche');	
		$filename = $this->request->data['lien_file']['tmp_name'];
		
		$extension = strtolower(pathinfo($this->request->data['lien_file']['name'], PATHINFO_EXTENSION));

		if(!empty($filename) && in_array($extension, array('xls')))
		{
		
					//lecture du fichier avec PHPExcel
		$objPHPExcel = PHPExcel_IOFactory::load($filename);
		
	
		foreach($objPHPExcel->getWorksheetIterator() as $worksheet)
		{
			$highestRow = $worksheet->getHighestRow();
			for($row=2; $row<=$highestRow; $row++)
			{
	
				$numContrat = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
				$prestataire = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
				$segment = $worksheet->getCellByColumnAndRow(3, $row)->getValue();
				$lotissement = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
				$libelle = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
				$valeurCible = $worksheet->getCellByColumnAndRow(6, $row)->getValue();
				$date_debut = implode('-', array_reverse(explode('/', $worksheet->getCellByColumnAndRow(7, $row)->getValue()))); 
				$date_fin = implode('-', array_reverse(explode('/', $worksheet->getCellByColumnAndRow(8, $row)->getValue()))); 
				$valeurRestanteCalculee = $worksheet->getCellByColumnAndRow(9, $row)->getValue();
				$montantCommande = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
				$montantReceptionne = $worksheet->getCellByColumnAndRow(11, $row)->getValue();

I manage to import the PHPEXCEL class without problem with APP :: import() on cakephp 2.6, but impossible on cakephp 3, I really need help thanks.

What’s the full namespaced name of the class? You use PHPExcel\Classes\PHPExcel\IOFactory but then reference PHPExcel_IOFactory. Those are different things.

1 Like

because in the IOFactory.php file, the class is named:

class PHPExcel_IOFactory
{

}

And what namespace is it in?

1 Like

thanks, i found the solution :
require_once(APP. ‘Vendor’.DS.‘PHPExcel’.DS.‘IOFactory.php’);
in my function importContrat:
$objPHPExcel = \PHPExcel_IOFactory::load($filename);

If this is the library I think it is - it would be a lot easier to add this as a composer dependency:

"phpoffice/phpexcel": "1.8.1",

I just ignore the warnings about it been abandoned - its does the job i need it to do…