public void WriteWorksheetPart(OpenXmlWriter writer) { // TODO: final cleanup // - merge redundant columns // - remove unused shared strings // Remove rows without cells foreach (var rowItem in _cachedCells.ToList()) { if (rowItem.Value.Count == 0) { _cachedCells.Remove(rowItem.Key); } } // Simulate rows for cached rows foreach (var rowIdx in _cachedRows.Keys) { if (!_cachedCells.ContainsKey(rowIdx)) { _cachedCells[rowIdx] = new SortedList <uint, CellProxy>(); } } // Get first and last addresses uint minRow = uint.MaxValue; uint minCol = uint.MaxValue; uint maxRow = 0; uint maxCol = 0; foreach (var rowItem in _cachedCells) { uint rowIdx = rowItem.Key; var cells = rowItem.Value; if (minRow == uint.MaxValue) { minRow = rowIdx; } maxRow = rowIdx; if (cells.Count > 0) { minCol = Math.Min(minCol, cells.Keys.First()); maxCol = Math.Max(maxCol, cells.Keys.Last()); } } string firstAddress = null, lastAddress = null; if (minRow < uint.MaxValue && minCol < uint.MaxValue) { firstAddress = RowColumn.ToAddress(minRow, minCol); if (minRow != maxRow || minCol != maxCol) { lastAddress = RowColumn.ToAddress(maxRow, maxCol); } } else { firstAddress = "A1"; } writer.WriteStartDocument(); writer.WriteStartElement(new Worksheet()); foreach (string childTagName in SchemaInfo.WorksheetChildSequence) { if (childTagName == "sheetData") { WriteSheetData(writer); } else if (childTagName == "dimension") { string dimensionRef = firstAddress + (lastAddress != null ? ":" + lastAddress : ""); writer.WriteElement(new SheetDimension() { Reference = dimensionRef }); } else if (childTagName == "sheetViews") { SheetViews svs = GetFirstElement <SheetViews>(); if (svs != null) { foreach (SheetView sv in svs.Elements <SheetView>()) { foreach (Selection sel in sv.Elements <Selection>()) { if (minRow < uint.MaxValue) { sel.ActiveCell = firstAddress; sel.SequenceOfReferences = new ListValue <StringValue>(new StringValue[] { new StringValue(firstAddress) }); } else { sel.Remove(); } } } writer.WriteElement(svs); } } else { foreach (var e in GetElementsByTagName(childTagName)) { writer.WriteElement(e); } } } writer.WriteEndElement(); // worksheet }
public void WriteWorksheetPart(OpenXmlWriter writer) { foreach (KeyValuePair <uint, SortedList <uint, CellProxy> > pair in this._cachedCells.ToList <KeyValuePair <uint, SortedList <uint, CellProxy> > >()) { if (pair.Value.Count == 0) { this._cachedCells.Remove(pair.Key); } } foreach (uint num in this._cachedRows.Keys) { if (!this._cachedCells.ContainsKey(num)) { this._cachedCells[num] = new SortedList <uint, CellProxy>(); } } uint maxValue = uint.MaxValue; uint num3 = uint.MaxValue; uint row = 0; uint num5 = 0; foreach (KeyValuePair <uint, SortedList <uint, CellProxy> > pair2 in this._cachedCells) { uint key = pair2.Key; SortedList <uint, CellProxy> list = pair2.Value; if (maxValue == uint.MaxValue) { maxValue = key; } row = key; if (list.Count > 0) { num3 = Math.Min(num3, list.Keys.First <uint>()); num5 = Math.Max(num5, list.Keys.Last <uint>()); } } string str = null; string str2 = null; if ((maxValue < uint.MaxValue) && (num3 < uint.MaxValue)) { str = RowColumn.ToAddress(maxValue, num3); if ((maxValue != row) || (num3 != num5)) { str2 = RowColumn.ToAddress(row, num5); } } else { str = "A1"; } writer.WriteStartDocument(); writer.WriteStartElement(new Worksheet()); foreach (string str3 in SchemaInfo.WorksheetChildSequence) { switch (str3) { case "sheetData": this.WriteSheetData(writer); break; case "dimension": { string str4 = str + ((str2 != null) ? (":" + str2) : ""); SheetDimension elementObject = new SheetDimension { Reference = str4 }; writer.WriteElement(elementObject); break; } case "sheetViews": { SheetViews firstElement = this.GetFirstElement <SheetViews>(); if (firstElement != null) { foreach (SheetView view in firstElement.Elements <SheetView>()) { foreach (Selection selection in view.Elements <Selection>()) { if (maxValue < uint.MaxValue) { selection.ActiveCell = str; selection.SequenceOfReferences = new ListValue <StringValue>(new StringValue[] { new StringValue(str) }); } else { selection.Remove(); } } } writer.WriteElement(firstElement); } break; } default: foreach (OpenXmlElement element in this.GetElementsByTagName(str3)) { writer.WriteElement(element); } break; } } writer.WriteEndElement(); }