New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can i merge two xlsx files and download the combined one? #1387
Comments
Hi @kurianic, the example below will read all the sheets from workbooks A and B, and copy them to a new workbook C. Please ensure A and B do not contain sheets with the same name. // Load the workbooks to merge in a collection.
// This example is assuming they're stored in the Laravel storage folder.
$workbooks = collect([
'workbookA.xlsx',
'workbookB.xlsx',
])->map(function ($filename) {
return Excel::load(storage_path($filename));
});
// Create merged workbook
$workbookC = Excel::create('workbookC', function ($excel) use ($workbooks) {
// For each workbook to be merged
$workbooks->each(function ($workbook) use ($excel) {
// Get all the sheets
collect($workbook->getAllSheets())->each(function ($sheet) use ($excel) {
// And add them to the merged workbook
$excel->addExternalSheet($sheet);
});
});
})->save(); // save merged workbook to storage/exports/workbookC.xlsx |
Hi @stephanecoinon , I want to add the same sheet multiple times, to prevent duplicated names, I called $sheet->setTitle. but it gives me an error "Sheet does not exist." when addExternalSheet called just after setTitle.
|
@sarpkaya-xx try Excel::create('workbook', function ($excel) {
// Create first "original" sheet
$original = $excel->sheet('Sheet 1', function ($sheet) {
$sheet->fromArray([
['foo' => 'bar']
]);
});
// Copy the first sheet 4 times
for ($sheetCount = 2; $sheetCount <= 5; $sheetCount++) {
$copy = $original->getSheet()->copy();
$copy->setTitle("Sheet {$sheetCount}");
$excel->addSheet($copy);
}
})->save(); |
Is there a similar functionality in version 3.0+ ? |
Please prefix your issue with one of the following: [BUG] [PROPOSAL] [QUESTION].
Package version, Laravel version
Expected behaviour
Actual behaviour
Exception stack trace
Screenshot of Excel file
Steps to reproduce the behaviour
The text was updated successfully, but these errors were encountered: