如何取消合并单元格EPPlus? [英] How to un-merge cells EPPlus?

查看:287
本文介绍了如何取消合并单元格EPPlus?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试取消合并,然后根据表列数重新合并较短或较长的范围 我在下面使用了此代码,但似乎不起作用

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;  

任何帮助将不胜感激.

推荐答案

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

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

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

有人为Merge属性发布了一个替换setter(尽管getter看起来仍然不正确).当我将其替换设置器粘贴到ExcelRangeBase.cs中并重新编译时,此测试方法开始为我工作:

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

如果您正在运行版本3,它应该可以正常工作,因此可以粘贴其余代码.

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

更新:来自Codeplex链接的代码.查找public bool Merge并替换集合.

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

这篇关于如何取消合并单元格EPPlus?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆