How to un-merge cells EPPlus?

北城余情 提交于 2020-01-04 02:53:06

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/27766973/how-to-un-merge-cells-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!