/// <summary> /// Recreate the workbook part with a new contenttype /// </summary> /// <param name="contentType">The new contenttype</param> private void ChangeContentTypeWorkbook(string contentType) { var p = _package.Package; var part = Part; var rels = part.GetRelationships(); p.DeletePart(WorkbookUri); part = p.CreatePart(WorkbookUri, contentType); foreach (var rel in rels) { p.DeleteRelationship(rel.Id); var newRel = part.CreateRelationship(rel.TargetUri, rel.TargetMode, rel.RelationshipType); if (rel.RelationshipType.EndsWith("worksheet")) { var sheetNode = (XmlElement)WorkbookXml.SelectSingleNode(string.Format("d:workbook/d:sheets/d:sheet[@r:id='{0}']", rel.Id), NameSpaceManager); sheetNode.SetAttribute("id", ExcelPackage.schemaRelationships, newRel.Id); } else if (rel.RelationshipType.EndsWith("pivotCacheDefinition")) { var sheetNode = (XmlElement)WorkbookXml.SelectSingleNode(string.Format("d:workbook/d:pivotCaches/d:pivotCache[@r:id='{0}']", rel.Id), NameSpaceManager); sheetNode.SetAttribute("id", ExcelPackage.schemaRelationships, newRel.Id); } } }
private void UpdateDefinedNamesXml() { try { XmlNode top = WorkbookXml.SelectSingleNode("//d:definedNames", NameSpaceManager); if (!ExistsNames()) { if (top != null) TopNode.RemoveChild(top); return; } else { if (top == null) { CreateNode("d:definedNames"); top = WorkbookXml.SelectSingleNode("//d:definedNames", NameSpaceManager); } else { top.RemoveAll(); } foreach (ExcelNamedRange name in _names) { XmlElement elem = WorkbookXml.CreateElement("definedName", ExcelPackage.schemaMain); top.AppendChild(elem); elem.SetAttribute("name", name.Name); if (name.IsNameHidden) elem.SetAttribute("hidden", "1"); if (!string.IsNullOrEmpty(name.NameComment)) elem.SetAttribute("comment", name.NameComment); SetNameElement(name, elem); } } foreach (ExcelWorksheet ws in _worksheets) { if (!(ws is ExcelChartsheet)) { foreach (ExcelNamedRange name in ws.Names) { XmlElement elem = WorkbookXml.CreateElement("definedName", ExcelPackage.schemaMain); top.AppendChild(elem); elem.SetAttribute("name", name.Name); elem.SetAttribute("localSheetId", name.LocalSheetId.ToString()); if (name.IsNameHidden) elem.SetAttribute("hidden", "1"); if (!string.IsNullOrEmpty(name.NameComment)) elem.SetAttribute("comment", name.NameComment); SetNameElement(name, elem); } } } } catch (Exception ex) { throw new Exception("Internal error updating named ranges ",ex); } }
internal void AddPivotTable(string cacheID, Uri defUri) { CreateNode("d:pivotCaches"); XmlElement item = WorkbookXml.CreateElement("pivotCache", ExcelPackage.schemaMain); item.SetAttribute("cacheId", cacheID); var rel = Part.CreateRelationship(UriHelper.ResolvePartUri(WorkbookUri, defUri), Packaging.TargetMode.Internal, ExcelPackage.schemaRelationships + "/pivotCacheDefinition"); item.SetAttribute("id", ExcelPackage.schemaRelationships, rel.Id); var pivotCaches = WorkbookXml.SelectSingleNode("//d:pivotCaches", NameSpaceManager); pivotCaches.AppendChild(item); }
private void ClearBookViews() { var node = WorkbookXml.SelectSingleNode("//d:bookViews/d:workbookView", NameSpaceManager); if (node != null) { var firstSheet = node.Attributes["firstSheet"]; if (firstSheet != null) { node.Attributes.Remove(firstSheet); } } }