How to merge cell using closedxml with a dynamic column count?
Asked Answered
C

3

6

This is how to merge cell using ClosedXML based on the documentation.

worksheet.Range("B2:D3").Row(1).Merge();

My problem is my column count is dynamic, I can't set column letter value to merge because i will based the merging of cell on my gridview column count.

Anyone who can help me to merge cell using closedXML?

Cobnut answered 23/9, 2016 at 1:44 Comment(1)
Please play fair and mark the question as answered if you're happy with the answer.Weksler
P
8

I do it this way

int row = 1;
int col = 1;

worksheet.Range(worksheet.Cell(row, col++), worksheet.Cell(row, col++)).Merge();
Pollster answered 27/8, 2019 at 14:30 Comment(0)
W
1

Build up the range as a variable based on your column count and pass the variable to the Range method. You don't need to HAVE to pass a hard coded value.

Weksler answered 23/9, 2016 at 7:0 Comment(0)
S
0

Have you tried the code below?

 worksheet.Cell("A1").Value = "Title";
 var range = worksheet.Range("A1:F1");
 range.Merge().Style.Font.SetBold().Font.FontSize = 16;

It works for me.

Seadon answered 30/3, 2017 at 3:32 Comment(2)
This is the same code as the question .Range("A1:F1") - maybe read the question?Chloric
Yeah I don't believe this answer provides additional context over the original question. One answer suggestion could be the following, where X is the amount of cells to merge on the given row: worksheet.Range(worksheet.Cell(row, col), worksheet.Cell(row, col + X)).Merge();Trump

© 2022 - 2024 — McMap. All rights reserved.