Search code examples
c#asp.netexcelworksheet

How can I merge cells of Excel package worksheet in C#?


I just want to merge number of cells in one column using worksheet of ExcelPackage; There is the way that I create my worksheet:

        var excelFile = new FileInfo(fileName);
        var package = new ExcelPackage(excelFile);
        var workSheet = package.Workbook.Worksheets.Add(sheetName);

So How can I merge cells in this worksheet? I will be thankful for any solution.


Solution

  • // Place data in cells
    worksheet.Cells["A1"].Value = "Cell A1";
    // Or [row, column] notation:
    worksheet.Cells[2,1].Value = "Cell A2";
    
    // Merge cells
    worksheet.Cells["A1:B1"].Merge = true;
    // Or [row, column] notation:
    worksheet.Cells[FromRow, FromColumn, ToRow, ToColumn].Merge = true;