I have two excel files each with a single worksheet. I want to take the two excel files and create a single excel file with two worksheets. I would like to do this with PHP. Can anyone point me in the right direction?
Asked
Active
Viewed 8,706 times
3
-
2The only thing that comes to mind is [PHPExcel](http://phpexcel.codeplex.com/). Not sure if it can do what you want though. – gen_Eric Jul 18 '12 at 16:24
-
Take a look at http://stackoverflow.com/questions/7286223/how-can-i-join-excel-documents-using-phpexcel – Jul 18 '12 at 16:24
-
1@Rocket - PHPExcel is quite capable of doing this – Mark Baker Jul 18 '12 at 16:25
-
@MarkBaker: Cool, never actually used it, but I figured it was a step in the right direction. – gen_Eric Jul 18 '12 at 16:27
1 Answers
5
Using PHPExcel
$inputFileType1 = 'Excel2007';
$inputFileName1 = 'inputData1.xlsx';
$inputFileType2 = 'Excel5';
$inputFileName2 = 'inputData2.xls';
$outputFileType = 'Excel5';
$outputFileName = 'outputData.xls';
// Load the first workbook (an xlsx file)
$objPHPExcelReader1 = PHPExcel_IOFactory::createReader($inputFileType1);
$objPHPExcel1 = $objPHPExcelReader1->load($inputFileName1);
// Load the second workbook (an xls file)
$objPHPExcelReader2 = PHPExcel_IOFactory::createReader($inputFileType2);
$objPHPExcel2 = $objPHPExcelReader2->load($inputFileName2);
// Merge the second workbook into the first
$objPHPExcel2->getActiveSheet()->setTitle('Unique worksheet name');
$objPHPExcel1->addExternalSheet($objPHPExcel2->getActiveSheet());
// Save the merged workbook under a new name (could save under the original name)
// as an xls file
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1,$outputFileType);
$objPHPExcelWriter->save($outputFileName);

Mark Baker
- 209,507
- 32
- 346
- 385
-
but it doesn't merge chart , try file with chart & graphs and it will remove that – Sooraz Nov 14 '17 at 12:24
-
This library is deprecated now use this: https://github.com/PHPOffice/PhpSpreadsheet – Hemant Kumar Dec 14 '18 at 05:05