覆蓋此問題的解決方案是在再次獲取合併單元地址之前保存文件,我已經在您提供的同一示例上嘗試了以下代碼,並且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