问题
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