我删除了一行,并且在此行错误后删除了合并单元格的地址。 例如,有合并的单元格" A78:C78"。如果我做
worksheet.DeleteRow(5)
的地址
worksheet.Cells[77,1]
是" A77"。但是
worksheet.MergedCells[77,1]
为null,但它应该是" A77:C77"。
的地址
worksheet.Cells[78,1]
是" A78"。但是
worksheet.MergedCells[78,1]
是" A77:C77",但它应该为空。 什么是有趣和奇怪的,有合并的细胞" A77:C77"在* .xlsx中保存的文档中。 我无法理解为什么会这样。 在删除合并单元格的正确地址的行之后,是否有必要以某种方式更新工作表?
答案 0 :(得分:1)
重写此问题的解决方案是在再次获取mergedcell地址之前保存文件,我在您提供的相同示例上尝试了以下代码,并且在package.Save()之后结果似乎是正确的。
using (var package = new ExcelPackage(new System.IO.FileInfo(filePath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
var address = worksheet.Cells[78, 1];//address is "A78"
var mergedadress = worksheet.MergedCells[78, 1];//address is "A78:C78"
worksheet.DeleteRow(5);
address = worksheet.Cells[77, 1];//address is "A77"
mergedaddress = worksheet.MergedCells[77, 1];//null
address = worksheet.Cells[78, 1];//address is "A78"
mergedaddress = worksheet.MergedCells[78, 1];//address is"A77:C77"
package.Save();
}
using (var package = new ExcelPackage(new System.IO.FileInfo(filePath)))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
var address = worksheet.Cells[77, 1];//address is "A77"
var mergedaddress = worksheet.MergedCells[77, 1];//Address is "A77:C77"
address = worksheet.Cells[78, 1];//address is "A78"
mergedaddress = worksheet.MergedCells[78, 1];//null
package.Save();
}
我使用过EPPLUS v4.1.1.0