diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs index 9ec97e6..1a3babf 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Load.cs @@ -188,6 +188,7 @@ (Columns)reader.LoadCurrentElement()); else if (reader.ElementType == typeof(Row)) { + lastRow = 0; LoadRows(s, numberingFormats, fills, borders, fonts, ws, sharedStrings, sharedFormulasR1C1, styleList, (Row)reader.LoadCurrentElement()); } @@ -716,12 +717,17 @@ sheetArea = sections[1]; } + private Int32 lastCell; private void LoadCells(SharedStringItem[] sharedStrings, Stylesheet s, NumberingFormats numberingFormats, Fills fills, Borders borders, Fonts fonts, Dictionary sharedFormulasR1C1, - XLWorksheet ws, Dictionary styleList, Cell cell) + XLWorksheet ws, Dictionary styleList, Cell cell, Int32 rowIndex) { Int32 styleIndex = cell.StyleIndex != null ? Int32.Parse(cell.StyleIndex.InnerText) : 0; - var xlCell = ws.CellFast(cell.CellReference); + + String cellReference = cell.CellReference == null + ? XLHelper.GetColumnLetterFromNumber(++lastCell) + rowIndex + : cell.CellReference.Value; + var xlCell = ws.CellFast(cellReference); if (styleList.ContainsKey(styleIndex)) xlCell.Style = styleList[styleIndex]; @@ -985,12 +991,14 @@ fontBase.VerticalAlignment = verticalTextAlignment.Val != null ? verticalTextAlignment.Val.Value.ToClosedXml() : XLFontVerticalTextAlignmentValues.Baseline; } + private Int32 lastRow; private void LoadRows(Stylesheet s, NumberingFormats numberingFormats, Fills fills, Borders borders, Fonts fonts, XLWorksheet ws, SharedStringItem[] sharedStrings, Dictionary sharedFormulasR1C1, Dictionary styleList, Row row) { - var xlRow = ws.Row((Int32) row.RowIndex.Value, false); + Int32 rowIndex = row.RowIndex == null ? ++lastRow : (Int32) row.RowIndex.Value; + var xlRow = ws.Row(rowIndex, false); if (row.Height != null) xlRow.Height = row.Height; @@ -1021,9 +1029,10 @@ } } + lastCell = 0; foreach (Cell cell in row.Elements()) LoadCells(sharedStrings, s, numberingFormats, fills, borders, fonts, sharedFormulasR1C1, ws, styleList, - cell); + cell, rowIndex); } private void LoadColumns(Stylesheet s, NumberingFormats numberingFormats, Fills fills, Borders borders, diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index 8cfe1f4..b6ec68e 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -3934,7 +3934,8 @@ cellsByRow[rowNum].Add(c); } - var sheetDataRows = sheetData.Elements().ToDictionary(r => (Int32) r.RowIndex.Value, r => r); + Int32 lastRow = 0; + var sheetDataRows = sheetData.Elements().ToDictionary(r => r.RowIndex == null ? ++lastRow : (Int32) r.RowIndex.Value, r => r); foreach ( var r in xlWorksheet.Internals.RowsCollection.Deleted.Where(r => sheetDataRows.ContainsKey(r.Key))) @@ -4005,8 +4006,10 @@ row.OutlineLevel = (byte) thisRow.OutlineLevel; } - - var cellsByReference = row.Elements().ToDictionary(c => c.CellReference.Value, c => c); + Int32 lastCell = 0; + var cellsByReference = row.Elements().ToDictionary(c => c.CellReference == null + ? XLHelper.GetColumnLetterFromNumber(++lastCell) + distinctRow + : c.CellReference.Value, c => c); foreach (var c in xlWorksheet.Internals.CellsCollection.Deleted.ToList()) {