I want to merge two xlsx file and download merged file in drupal 8. So i used a PHPExcel library for doing so.

But i am not able to include a PHPExcel.php file in custom controller for that. I used require_once, module_load_include() but does not work for me.

How can i include it . please help.

require_once DRUPAL_ROOT.'/'.drupal_get_path('module','custom').'/src/Controller/PHPExcel.php';


module_load_include('php', 'custom', 'src/Controller/PHPExcel');

$file1 = 'ford.xlsx';
$file2 = 'toyota.xlsx';
$objPHPExcel1 = PHPExcel_IOFactory::load($file1);
$objPHPExcel2 = PHPExcel_IOFactory::load($file2);

foreach($objPHPExcel2->getSheetNames() as $sheetName) {
$sheet = $objPHPExcel2->getSheetByName($sheetName);

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="ford-toyota.xls"');
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); 
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 

and error is :

Fatal error: Class 'Drupal\custom\Controller\PHPExcel_IOFactory' not found

I am not sure from your question which phpexcel library you are using, so there are potentially different answers, here is one:

1. On Packagist

If like in the case of mvlabs/mvlabs-phpexcel you would do the following:

on the command line:

composer require mvlabs/mvlabs-phpexcel

Then in the file where you are wanting to use it you can simply add a "Use" statement (as per this answer):

use ExternalPackage\namespace\Foo;

Drupal 8 does an auto-include and is "aware" of all the PHP libraries inside the vendor folder.

  • i used composer require phpoffice/phpexcel
    – user78498
    Sep 28 '17 at 7:23