如何取消合并单元格 EPPlus?

How to un-merge cells EPPlus?

我正在尝试取消合并并重新合并更短或更长的范围,具体取决于 table 列的数量 我在下面使用了这段代码,但它似乎不起作用

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

非常感谢任何帮助。

你是 运行 EPP 4.0.1 吗?如果是这样,这是一个已知问题:

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

有人发布了 setter 替换 Merge 属性(尽管 getter 似乎仍然不正确)。当我将它们的替换 setter 粘贴到 ExcelRangeBase.cs 并重新编译时,此测试方法开始对我起作用:

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

如果您是 运行 版本 3,它应该可以正常工作,所以可以粘贴其余代码。

更新:codeplex link 中的代码。查找 public bool Merge 并替换集合。

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