Skip to content
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

Closed
kurianic opened this issue Oct 9, 2017 · 4 comments
Closed

How can i merge two xlsx files and download the combined one? #1387

kurianic opened this issue Oct 9, 2017 · 4 comments

Comments

@kurianic
Copy link

kurianic commented Oct 9, 2017

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

@stephanecoinon
Copy link

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

@sarpkaya-xx
Copy link

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.

$sheet_original->setTitle("SHEET ".$i); $sheets[$i] = $sheet_original; $sheets[$i]->cell('I64', function($cell) use($i) { $cell->setValue($i); }); $excel->addExternalSheet($sheets[$i] );

@stephanecoinon
Copy link

stephanecoinon commented Feb 11, 2018

@sarpkaya-xx try addSheet():

    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();

@BFRS01923
Copy link

BFRS01923 commented Apr 15, 2024

Is there a similar functionality in version 3.0+ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants