How To Merged or Combined Two or More Excel File(.xls) Using PHPExcel Library

This tutorial explains in a easy way to merged or combined multiple Excel File into a single File with multiple Sheet using PHPExcel Library.

0

This tutorial explains in a easy way to merged or combined multiple Excel File into a single File with multiple Sheet using PHPExcel Library.

 


Introduction

To combined multiple Excel File into a single Excel File with multiple Sheet in PHP, we need to rename all sheet name of all Excel File. Default Sheet name is Sheet1 but we will change this name, whatever you want. We will use PHPReport Library to merge Excel Sheet. Include this Library at the top of the file.

Rename Excel Sheet
Rename Excel Sheet

 

Download PHPReport Library

We need to download PHPReport Library and extract this downloaded file. Include PHPReport.php Library into Project file.
 

PHP Code To Merge Excel File

<?php require_once('PhpReport/PHPReport.php'); 


// Files are loaded to PHPExcel using the IOFactory load() method
$objPHPExcel1 = PHPExcel_IOFactory::load("/Your Excel File Path/example1.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("/Your Excel File Path2/example2.xlsx");
$objPHPExcel3 = PHPExcel_IOFactory::load("/Your Excel File Path2/example3.xlsx");

// Copy worksheets from $objPHPExcel2 to $objPHPExcel1
foreach($objPHPExcel2->getAllSheets() as $sheet) {
   $objPHPExcel1->addExternalSheet($sheet);
}

// Copy worksheets from $objPHPExcel3 to $objPHPExcel1
foreach($objPHPExcel3->getAllSheets() as $sheet1) {
   $objPHPExcel1->addExternalSheet($sheet1);
}

// Save $objPHPExcel1 to browser as an .xls file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, "Excel2007");
        
$objWriter->save("/Your Excel File Path2/example3.xlsx");

?>