如何拆分细胞EPPlus?

时间:2015-01-04 15:18:57

标签: c# epplus epplus-4

我试图取消合并并重新合并更短或更长的范围,具体取决于表格列的数量 我在下面使用了这段代码,但它似乎无法正常工作

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

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:3)

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

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

有人为Merge属性发布了替换setter(虽然看起来getter仍然不正确)。当我在ExcelRangeBase.cs中粘贴他们的替换setter并重新编译时,这个测试方法开始为我工作:

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