Merge cells using EPPlus?

Steven picture Steven · May 30, 2011 · Viewed 72.9k times · Source

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?

Answer

Carles Company picture Carles Company · May 30, 2011

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;
}