public void WriteWorksheet(OpenXmlWriter writer, WorksheetPart part, SpreadsheetStylesManager stylesManager, ref int tableCount) { var hyperLinksManager = new SpreadsheetHyperlinkManager(); writer.WriteStartElement(new Worksheet(), new List <OpenXmlAttribute>(), new List <KeyValuePair <string, string> >() { new KeyValuePair <string, string>("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships") }); writeSheetProperties(writer); writeFrozenFirstColumn(writer); writeColumns(writer); writeSheetData(writer, stylesManager, hyperLinksManager, DrawingsManager); writeMergedCells(writer); writeHyperlinks(writer, part, hyperLinksManager); writeDrawings(part, writer); writeTables(writer, part, ref tableCount); writeExtensionsList(writer); writer.WriteEndElement(); }
public override void WriteCell(OpenXmlWriter writer, int columnIndex, int rowIndex, SpreadsheetStylesManager stylesManager, SpreadsheetHyperlinkManager hyperlinkManager) { base.WriteCell(writer, columnIndex, rowIndex, stylesManager, hyperlinkManager); hyperlinkManager.AddHyperlink(new SpreadsheetLocation(rowIndex, columnIndex), _hyperLink); }
private void writeSheetData(OpenXmlWriter writer, SpreadsheetStylesManager stylesManager, SpreadsheetHyperlinkManager hyperlinkManager, DrawingsManager drawingsManager) { writer.WriteStartElement(new SheetData()); foreach (var row in _rows) { row.Value.WriteRow(writer, row.Key.ColumnIndex, row.Key.RowIndex, stylesManager, hyperlinkManager, drawingsManager); } foreach (var table in _tables) { if (!table.Value.IsInStreamingMode) { continue; } using (var enumerator = table.Value.GetStreamingEnumerator()) { var tableRowPosition = table.Value.StreamedRowsSoFar; while (enumerator.MoveNext()) { var row = enumerator.Current; row.WriteRow(writer, table.Key.ColumnIndex, table.Key.RowIndex + tableRowPosition + 1, stylesManager, hyperlinkManager, drawingsManager); tableRowPosition++; } } } writer.WriteEndElement(); }
private void writeHyperlinks(OpenXmlWriter writer, WorksheetPart woorksheetPart, SpreadsheetHyperlinkManager hyperlinkManager) { var hyperlinks = hyperlinkManager.GetHyperlinks(); if (!hyperlinks.Any()) { return; } var hyperlinkTargetRelationshipIds = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); var clickableHyperLinks = new HashSet <KeyValuePair <SpreadsheetLocation, SpreadsheetHyperLink> >(); // we must add external URLs as relationships and reference them in the hyperlinks // the more unique URLs there are the more performance problems there are // because of this, a limit of INT_MaxUniqueHyperlinks was put in place // without it the export can take 10x longer and it only gets worse the more unique links there are // why is that? // when adding a new relationship there is a uniqueness check. The check is performed sequentialy so the actual impact i O(n^2) // also this part does not seem streamable so all of the openxml element would need to be stored in the memory foreach (var hyperlink in hyperlinks.Where(x => !string.IsNullOrEmpty(x.Value.Target))) { var target = hyperlink.Value.Target; if (hyperlinkTargetRelationshipIds.Count > MaxUniqueHyperlinks) { break; } clickableHyperLinks.Add(hyperlink); if (!hyperlinkTargetRelationshipIds.ContainsKey(target)) { var uri = Utilities.SafelyCreateUri(target); if (uri == null) { hyperlinkTargetRelationshipIds[target] = ""; continue; } var relId = woorksheetPart.AddHyperlinkRelationship(uri, true).Id; hyperlinkTargetRelationshipIds[target] = relId; } } if (clickableHyperLinks.Count == 0) { return; } writer.WriteStartElement(new Hyperlinks()); foreach (var link in clickableHyperLinks) { var attributes = new List <OpenXmlAttribute>(); attributes.Add(new OpenXmlAttribute("ref", null, string.Format("{0}{1}", SpreadsheetHelper.ExcelColumnFromNumber(link.Key.ColumnIndex), link.Key.RowIndex))); string id; if (hyperlinkTargetRelationshipIds.TryGetValue(link.Value.Target, out id) && !string.IsNullOrEmpty(id)) { var idAtt = new OpenXmlAttribute("r", "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships", hyperlinkTargetRelationshipIds[link.Value.Target]); attributes.Add(idAtt); } else { attributes.Add(new OpenXmlAttribute("location", null, link.Value.Target)); attributes.Add(new OpenXmlAttribute("display", null, link.Value.DisplayValue)); } writer.WriteStartElement(new Hyperlink(), attributes); writer.WriteEndElement(); } writer.WriteEndElement(); }
public void WriteRow(OpenXmlWriter writer, int columnIndex, int rowIndex, SpreadsheetStylesManager stylesManager, SpreadsheetHyperlinkManager hyperlinkManager, DrawingsManager drawingsManager) { var span = string.Format("{0}:{1}", columnIndex, RowCells.Count + columnIndex); var attributeList = new List <OpenXmlAttribute>(); var rowIndexAtt = new OpenXmlAttribute("r", null, rowIndex.ToString()); var spanAtt = new OpenXmlAttribute("spans", null, span); attributeList.Add(rowIndexAtt); attributeList.Add(spanAtt); if (this.RowHeight != null) { attributeList.Add(new OpenXmlAttribute("customHeight", null, "1")); attributeList.Add(new OpenXmlAttribute("ht", null, this.RowHeight.Value.ToString(CultureInfo.InvariantCulture))); } if (!IsVisible) { var hiddenAttribute = new OpenXmlAttribute("hidden", null, 1.ToString()); attributeList.Add(hiddenAttribute); } if (!IsExpanded) { var hiddenAttribute = new OpenXmlAttribute("collapsed", null, 1.ToString()); attributeList.Add(hiddenAttribute); } if (!IsMaster) { var hiddenAttribute = new OpenXmlAttribute("outlineLevel", null, 1.ToString()); attributeList.Add(hiddenAttribute); } writer.WriteStartElement(new Row(), attributeList); foreach (var cell in RowCells) { if (cell.ImageIndex != -1) { drawingsManager.SetImageForCell(new ImageDetails() { Column = columnIndex, ImageIndex = cell.ImageIndex, ImageScaleFactor = cell.ImageScaleFactor, Indent = cell.Indent, Row = rowIndex }); } cell.WriteCell(writer, columnIndex, rowIndex, stylesManager, hyperlinkManager); columnIndex++; } writer.WriteEndElement(); }