3

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

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

任何帮助将不胜感激。

4

1 回答 1

3

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

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

有人发布了该Merge属性的替换 setter(尽管 getter 似乎仍然不正确)。当我将他们的替换设置器粘贴到 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 链接的代码。寻找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);
            }
        }
    }
}
于 2015-01-05T00:19:08.207 回答