Merge

I need to merge rows in the columns B and C from row 8 to 30

Now i use like...

sheet.Range("B8:C8").Merge()
sheet.Range("B9:C9").Merge()
sheet.Range("B10:C10").Merge()
and so on to line 30.............


Is there any better way to do it?


2 Replies

MC Mohan Chandran Syncfusion Team November 16, 2023 11:08 AM UTC

Hi Kjell,

We request you use the IRange indexer to select cells and use it in a loop to merge more than one row. Please refer to the code snippet below.

using System.Diagnostics;

using Syncfusion.XlsIO;


namespace MergeCells

{

    internal class Program

    {

        static void Main(string[] args)

        {

            //Create Excel document using XlsIO

            ExcelEngine excelEngine = new ExcelEngine();

            IApplication application = excelEngine.Excel;

            application.DefaultVersion = ExcelVersion.Xlsx;


            IWorkbook workbook = application.Workbooks.Create(1);


            IWorksheet worksheet = workbook.Worksheets[0];


            int firstRow = 8;

            int lastRow = 30;


            int firstColumn = 2;

            int lastColumn = 3;


            //Merge cells

            for(int row = firstRow; row <= lastRow; row++)

            {

               worksheet.Range[row, firstColumn, row, lastColumn].Merge();

            }


            worksheet[firstRow, firstColumn, lastRow, lastColumn].BorderAround();


            workbook.SaveAs("Output.xlsx");


            Process.Start("Output.xlsx");

        }

    }

}


Regards,

Mohan.



KE Kjell Ek November 16, 2023 04:46 PM UTC

Thanks :-)


Loader.
Up arrow icon