3

I'm trying to un-merge and re-merge shorter or longer range depending on the the number of table columns I used this code below but it doesnt seem to work

tableSheet.Cells["C1:J1"].Merge = false;  

Any help will be highly appreciated.

Corey Adler
  • 15,897
  • 18
  • 66
  • 80
user2967602
  • 33
  • 1
  • 3

1 Answers1

3

Are you running EPP 4.0.1? If so, it is a known issue:

https://epplus.codeplex.com/workitem/15134

Someone posted a replacement setter for the Merge property (although the getter is still incorrect it seems). When I pasted their replacement setter in ExcelRangeBase.cs and recompiled this test method started working for me:

[TestMethod]
public void Merge_Unmerge_Cells_Test()
{
    var existingFile = new FileInfo(@"c:\temp\temp.xlsx");
    if (existingFile.Exists)
        existingFile.Delete();

    using (var package = new ExcelPackage(existingFile))
    {
        var workbook = package.Workbook;
        var worksheet = workbook.Worksheets.Add("newsheet");

        worksheet.Select("A1:C3");
        worksheet.SelectedRange.Merge = true;
        worksheet.SelectedRange.Value = "Merged Value";

        worksheet.SelectedRange.Merge = false;

        package.Save();
    }
}

If you are running version 3 it should just work so maybe paste the rest of your code.

UPDATE: The code from the codeplex link. Look for public bool Merge and replace the set.

set
{
    IsRangeValid("merging");
    //SetMerge(value, FirstAddress);
    if (!value)
    {
       if (_worksheet.MergedCells.Contains(FirstAddress))
       {
           _worksheet.MergedCells.Remove(FirstAddress);
       }
       else
       {
           throw (new ArgumentException("Range is not merged"));
       }
       if (Addresses != null)
       {
           foreach (var address in Addresses)
           {
               if (_worksheet.MergedCells.Contains(address.Address))
               {
                   _worksheet.MergedCells.Remove(address.Address);
               }
               else
               {
                   throw (new ArgumentException("Range is not merged"));
               }
           }
       }
    }
    else
    {
        _worksheet.MergedCells.Add(new ExcelAddressBase(FirstAddress), true);
        if (Addresses != null)
        {
            foreach (var address in Addresses)
            {
                _worksheet.MergedCells.Add(address, true);
                //SetMerge(value, address._address);
            }
        }
    }
}
Corey Adler
  • 15,897
  • 18
  • 66
  • 80
Ernie S
  • 13,902
  • 4
  • 52
  • 79
  • Thanks for the answer, yes I'm using 4.0.1, do you have the code that u pasted in excelRangeBase.cs, is it the one in the link? – user2967602 Jan 05 '15 at 02:58
  • @user2967602 : see my edit above. I pasted in the code - just find public bool Merge and replace its set. – Ernie S Jan 05 '15 at 11:47
  • @user2967602 : No problem. Just remember to mark the this answer as complete to close out the question. Also, it would be a good idea to log into Codeplex and upvote that issue since you are experiencing it so it gets more attention (I already did :) ). – Ernie S Jan 05 '15 at 15:43