Merge cells using EPPlus?

86,248

Solution 1

You have to use it like this:

ws.Cells["A1:C1"].Merge = true;

instead of:

using (ExcelRange rng = ws.Cells["A1:C1"])
{
    bool merge = rng.Merge;
}

Solution 2

If you want to merge cells dynamically, you can also use:

worksheet.Cells[FromRow, FromColumn, ToRow, ToColumn].Merge = true;

All these variables are integers.

Solution 3

You can create a extension method:

public static void Merge(this ExcelRangeBase range)
{
    ExcelCellAddress start = range.Start;
    ExcelCellAddress end = range.End;
    range.Worksheet.Cells[start.Row, start.Column, end.Row, end.Column].Merge = true;
}

You can use this as you would via interop:

range.Merge();
Share:
86,248

Related videos on Youtube

Steven
Author by

Steven

Updated on April 12, 2022

Comments

  • Steven
    Steven about 2 years

    I'm using the EPPlus library to read/write Excel files: http://epplus.codeplex.com/

    I'm trying to simply merge some cells when writing a document:

    using (ExcelPackage pck = new ExcelPackage())
    {
        //Create the worksheet
        ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
    
        //Format the header for column 1-3
        using (ExcelRange rng = ws.Cells["A1:C1"])
        {
            bool merge = rng.Merge;
        }
    }
    

    There's a property named Merge that simply returns true or false. I thought maybe that would Merge the cells, but it doesn't.

    Anyone know how to do this?

  • Hari
    Hari about 10 years
    How can i pass the Dynamic parameters to merge the cells in vb.net
  • Nikos
    Nikos over 5 years
    Would this work if I wanted to do the opposite? Unmerge cells.
  • Prashant Pimpale
    Prashant Pimpale over 5 years
    How do I merge columns on the basis of condition?
  • Carles Company
    Carles Company over 5 years
    @PrashantPimpale I don't understand your question. Have in mind that cells in the range you want to merge need to be adjacent.
  • Prashant Pimpale
    Prashant Pimpale over 5 years
    I have posted a question here with the details: stackoverflow.com/q/52703122/7124761
  • curiousBoy
    curiousBoy over 5 years
    Just as an additional info here. Only the most top left cell value will be kept after merge (the rest of the cell values will be ignored)