diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index a761ef1..fa92a1a 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -4642,6 +4642,13 @@ xlWorksheet.Internals.CellsCollection.deleted.Remove(r.Key); } + var tableTotalCells = new HashSet( + xlWorksheet.Tables + .Where(table => table.ShowTotalsRow) + .SelectMany(table => + table.TotalsRow().CellsUsed()) + .Select(cell => cell.Address)); + var distinctRows = xlWorksheet.Internals.CellsCollection.RowsCollection.Keys.Union(xlWorksheet.Internals.RowsCollection.Keys); var noRows = !sheetData.Elements().Any(); foreach (var distinctRow in distinctRows.OrderBy(r => r)) @@ -4787,7 +4794,7 @@ cell.CellValue = null; } - else if (xlCell.TableCellType() == XLTableCellType.Total) + else if (tableTotalCells.Contains(xlCell.Address)) { var table = xlWorksheet.Tables.First(t => t.AsRange().Contains(xlCell)); field = table.Fields.First(f => f.Column.ColumnNumber() == xlCell.Address.ColumnNumber) as XLTableField;