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 to merge cells? #296

Open
afiqiqmal opened this issue Nov 2, 2022 · 1 comment
Open

How to merge cells? #296

afiqiqmal opened this issue Nov 2, 2022 · 1 comment

Comments

@afiqiqmal
Copy link

I see that box spout has merge cell function but i can't figure it out where to customize it in fast-excel. How to merge cell in fast-excel?

@HussamAlhennawi
Copy link

HussamAlhennawi commented Jan 23, 2024

Here is a workaround solution by using configureOptionsUsing() with a callback.

You can use mergeCells() function from vendor\openspout\openspout\src\Writer\XLSX\Options.php:

public function mergeCells(
    int $topLeftColumn,
    int $topLeftRow,
    int $bottomRightColumn,
    int $bottomRightRow,
    int $sheetIndex = 0,
)

Example:

$firstRow = [
    "First Column",     // will be merged vertically with the cell below it
    "Second Column",    // will be merged horizontally with the right cell
    "",                 // this value will be ignored after merge
    "Fourth Column",    // will be merged horizontally with the right cell
    ""                  // this value will be ignored after merge
];

$excelRows = collect([$firstRow, ...]);

return (new FastExcel($excelRows))
    ->configureOptionsUsing(function ($options) {
        $options->mergeCells('0', '1', '0', '2');
        $options->mergeCells('1', '1', '2', '1');
        $options->mergeCells('3', '1', '4', '1');
    })
    ->withoutHeaders();

Tip: In the mergeCells() parameters the rows started from 1 and columns from 0.

The result will be like this:

Merge Cells

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

2 participants