Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two or more xls files as worksheets PHPExcel

Tags:

php

phpexcel

I've been looking everywhere on how to do this with two existing files, looks like all documentation is on creating new files. I'd like to take one of the files and add the second file to it as a new worksheet then save it to the server. I've been trying with no avail like this:

$file="test.xls";
$file2="test2.xls";
$outputFile = "final.xls";
$phpExcel = new PHPExcel($file);
$phpExcel->getActiveSheet();
$phpExcel->setActiveSheetIndex(0);
$phpExcel->addSheet($file2);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$outputFile");
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, "Excel5");
file_put_contents($outputFile, $objWriter);

Any help would be greatly appreciated. Very new to PHP.

like image 325
user2430227 Avatar asked May 28 '13 21:05

user2430227


People also ask

How to combine multiple Excel files into sheets?

With the master workbook open, go to the Ablebits Data tab > Merge group, and click Copy Sheets > Selected Sheets to one Workbook. In the Copy Worksheets dialog window, select the files (and optionally worksheets) you want to merge and click Next. Select one or more additional options, if needed, and click Copy.

How can I create multiple sheets in Excel using Phpexcel?

When you first instantiate the $objPHPExcel, it already has a single sheet (sheet 0); you're then adding a new sheet (which will become sheet 1), but setting active sheet to sheet $i (when $i is 0)... so you're renaming and populating the original worksheet created when you instantiated $objPHPExcel rather than the one ...

How can I recover data from Excel in asp net?

First of all, open a new Excel Sheet and enter the information that you want to add. Now start Visual Studio and create a Web Application. Right-click on this application and add the Excel File to this application. Now you need to add a Drop Down List, a List Item, a Label, and a Grid View to your application.


1 Answers

Doesn't anybody ever read documentation these days? There's a whole document in the folder called /Documentation about reading files to PHPExcel objects (it's called PHPExcel User Documentation - Reading Spreadsheet Files), together with dozens of examples (the /Documentation/Examples/Reader folder is a good place to look), and none of them use new PHPExcel($file). Nor do any of the examples or any of the documents say to use file_put_contents() when saving.

$file1="test.xls";
$file2="test2.xls";
$outputFile = "final.xls";

// Files are loaded to PHPExcel using the IOFactory load() method
$objPHPExcel1 = PHPExcel_IOFactory::load($file1);
$objPHPExcel2 = PHPExcel_IOFactory::load($file2);

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

// Save $objPHPExcel1 to browser as an .xls file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, "Excel5");
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$outputFile");
header("Cache-Control: max-age=0");
$objWriter->save('php://output');
like image 129
Mark Baker Avatar answered Dec 04 '22 09:12

Mark Baker