private void CopyVmlDrawing(ExcelWorksheet origSheet, ExcelWorksheet newSheet) { var xml = origSheet.VmlDrawingsComments.VmlDrawingXml.OuterXml; var vmlUri = new Uri(string.Format("/xl/drawings/vmlDrawing{0}.vml", newSheet.SheetID), UriKind.Relative); var part = _pck.Package.CreatePart(vmlUri, "application/vnd.openxmlformats-officedocument.vmlDrawing", _pck.Compression); using (var streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write))) { streamDrawing.Write(xml); } //Add the relationship ID to the worksheet xml. PackageRelationship vmlRelation = newSheet.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(newSheet.WorksheetUri, vmlUri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/vmlDrawing", PackagePartForMono.NextRelationshipID); var e = newSheet.WorksheetXml.SelectSingleNode("//d:legacyDrawing", _namespaceManager) as XmlElement; if (e == null) { e = newSheet.WorksheetXml.CreateNode(XmlNodeType.Entity, "//d:legacyDrawing", _namespaceManager.LookupNamespace("d")) as XmlElement; } if (e != null) { e.SetAttribute("id", ExcelPackage.schemaRelationships, vmlRelation.Id); } }
private XmlDocument GetXmlDocument(string startXml, Uri uri, string contentType, string relationship) { XmlDocument xmlDoc; if (_package.Package.PartExists(uri)) { xmlDoc = _package.GetXmlFromUri(uri); } else { xmlDoc = new XmlDocument(); xmlDoc.LoadXml(startXml); // Create a the part and add to the package PackagePart part = _package.Package.CreatePart(uri, contentType); // Save it to the package StreamWriter stream = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); xmlDoc.Save(stream); stream.Close(); _package.Package.Flush(); // create the relationship between the workbook and the new shared strings part _package.Package.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(new Uri("/xl", UriKind.Relative), uri), TargetMode.Internal, relationship, PackagePartForMono.NextRelationshipID); _package.Package.Flush(); } return(xmlDoc); }
private void CreateBlankWb() { XmlDocument workbook = Workbook.WorkbookXml; // this will create the workbook xml in the package // create the relationship to the main part _package.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(new Uri("/xl", UriKind.Relative), Workbook.WorkbookUri), TargetMode.Internal, schemaRelationships + "/officeDocument", PackagePartForMono.NextRelationshipID); }
private void CopyComment(ExcelWorksheet Copy, ExcelWorksheet workSheet) { //First copy the drawing XML string xml = Copy.Comments.CommentXml.InnerXml; var uriComment = new Uri(string.Format("/xl/comments{0}.xml", workSheet.SheetID), UriKind.Relative); if (_pck.Package.PartExists(uriComment)) { uriComment = XmlHelper.GetNewUri(_pck.Package, "/xl/drawings/vmldrawing{0}.vml"); } var part = _pck.Package.CreatePart(uriComment, "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", _pck.Compression); StreamWriter streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamDrawing.Write(xml); streamDrawing.Close(); //Add the relationship ID to the worksheet xml. PackageRelationship commentRelation = workSheet.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(workSheet.WorksheetUri, uriComment), TargetMode.Internal, ExcelPackage.schemaRelationships + "/comments", PackagePartForMono.NextRelationshipID); xml = Copy.VmlDrawingsComments.VmlDrawingXml.InnerXml; var uriVml = new Uri(string.Format("/xl/drawings/vmldrawing{0}.vml", workSheet.SheetID), UriKind.Relative); if (_pck.Package.PartExists(uriVml)) { uriVml = XmlHelper.GetNewUri(_pck.Package, "/xl/drawings/vmldrawing{0}.vml"); } var vmlPart = _pck.Package.CreatePart(uriVml, "application/vnd.openxmlformats-officedocument.vmlDrawing", _pck.Compression); StreamWriter streamVml = new StreamWriter(vmlPart.GetStream(FileMode.Create, FileAccess.Write)); streamVml.Write(xml); streamVml.Close(); PackageRelationship newVmlRel = workSheet.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(workSheet.WorksheetUri, uriVml), TargetMode.Internal, ExcelPackage.schemaRelationships + "/vmlDrawing", PackagePartForMono.NextRelationshipID); //Add the relationship ID to the worksheet xml. XmlElement e = workSheet.WorksheetXml.SelectSingleNode("//d:legacyDrawing", _namespaceManager) as XmlElement; if (e == null) { workSheet.CreateNode("d:legacyDrawing"); e = workSheet.WorksheetXml.SelectSingleNode("//d:legacyDrawing", _namespaceManager) as XmlElement; } e.SetAttribute("id", ExcelPackage.schemaRelationships, newVmlRel.Id); }
string CreateWorkbookRel(string Name, int sheetID, Uri uriWorksheet) { //Create the relationship between the workbook and the new worksheet PackageRelationship rel = _pck.Workbook.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(_pck.Workbook.WorkbookUri, uriWorksheet), TargetMode.Internal, ExcelPackage.schemaRelationships + "/worksheet", PackagePartForMono.NextRelationshipID); _pck.Package.Flush(); //Create the new sheet node XmlElement worksheetNode = _pck.Workbook.WorkbookXml.CreateElement("sheet", ExcelPackage.schemaMain); worksheetNode.SetAttribute("name", Name); worksheetNode.SetAttribute("sheetId", sheetID.ToString()); worksheetNode.SetAttribute("id", ExcelPackage.schemaRelationships, rel.Id); TopNode.AppendChild(worksheetNode); return(rel.Id); }
private void UpdateSharedStringsXml() { PackagePart stringPart; if (_package.Package.PartExists(SharedStringsUri)) { stringPart = _package.Package.GetPart(SharedStringsUri); } else { stringPart = _package.Package.CreatePart(SharedStringsUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml", _package.Compression); Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(WorkbookUri, SharedStringsUri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/sharedStrings", PackagePartForMono.NextRelationshipID); } StreamWriter sw = new StreamWriter(stringPart.GetStream(FileMode.Create, FileAccess.Write)); sw.Write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?><sst xmlns=\"http://schemas.openxmlformats.org/spreadsheetml/2006/main\" count=\"{0}\" uniqueCount=\"{0}\">", _sharedStrings.Count); foreach (string t in _sharedStrings.Keys) { SharedStringItem ssi = _sharedStrings[t]; if (ssi.isRichText) { sw.Write("<si>"); ExcelEncodeString(sw, t); sw.Write("</si>"); } else { if (t.Length > 0 && (t[0] == ' ' || t[t.Length - 1] == ' ' || t.Contains(" ") || t.Contains("\t"))) { sw.Write("<si><t xml:space=\"preserve\">"); } else { sw.Write("<si><t>"); } ExcelEncodeString(sw, SecurityElement.Escape(t)); sw.Write("</t></si>"); } } sw.Write("</sst>"); sw.Flush(); }
internal void SaveHeaderFooterImages() { if (_vmlDrawingsHF != null) { if (_vmlDrawingsHF.Count == 0) { if (_vmlDrawingsHF.Uri != null) { _ws.Part.DeleteRelationship(_vmlDrawingsHF.RelId); _ws._package.Package.DeletePart(_vmlDrawingsHF.Uri); } } else { if (_vmlDrawingsHF.Uri == null) { _vmlDrawingsHF.Uri = XmlHelper.GetNewUri(_ws._package.Package, @"/xl/drawings/vmlDrawing{0}.vml"); } if (_vmlDrawingsHF.Part == null) { _vmlDrawingsHF.Part = _ws._package.Package.CreatePart(_vmlDrawingsHF.Uri, "application/vnd.openxmlformats-officedocument.vmlDrawing", _ws._package.Compression); var rel = _ws.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(_ws.WorksheetUri, _vmlDrawingsHF.Uri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/vmlDrawing", PackagePartForMono.NextRelationshipID); _ws.SetHFLegacyDrawingRel(rel.Id); _vmlDrawingsHF.RelId = rel.Id; foreach (ExcelVmlDrawingPicture draw in _vmlDrawingsHF) { rel = _vmlDrawingsHF.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(_vmlDrawingsHF.Uri, draw.ImageUri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image", PackagePartForMono.NextRelationshipID); draw.RelId = rel.Id; } } _vmlDrawingsHF.VmlDrawingXml.Save(_vmlDrawingsHF.Part.GetStream()); } } }
private void CopyDrawing(ExcelWorksheet Copy, ExcelWorksheet workSheet /*, PackageRelationship r*/) { //Check if the worksheet has drawings //if(_xlPackage.Package.PartExists(r.TargetUri)) //{ //First copy the drawing XML string xml = Copy.Drawings.DrawingXml.OuterXml; var uriDraw = new Uri(string.Format("/xl/drawings/drawing{0}.xml", workSheet.SheetID), UriKind.Relative); var part = _pck.Package.CreatePart(uriDraw, "application/vnd.openxmlformats-officedocument.drawing+xml", _pck.Compression); StreamWriter streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamDrawing.Write(xml); streamDrawing.Close(); XmlDocument drawXml = new XmlDocument(); drawXml.LoadXml(xml); //Add the relationship ID to the worksheet xml. PackageRelationship drawRelation = workSheet.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(workSheet.WorksheetUri, uriDraw), TargetMode.Internal, ExcelPackage.schemaRelationships + "/drawing", PackagePartForMono.NextRelationshipID); XmlElement e = workSheet.WorksheetXml.SelectSingleNode("//d:drawing", _namespaceManager) as XmlElement; e.SetAttribute("id", ExcelPackage.schemaRelationships, drawRelation.Id); foreach (ExcelDrawing draw in Copy.Drawings) { if (draw is ExcelChart) { ExcelChart chart = draw as ExcelChart; xml = chart.ChartXml.InnerXml; var UriChart = XmlHelper.GetNewUri(_pck.Package, "/xl/charts/chart{0}.xml"); var chartPart = _pck.Package.CreatePart(UriChart, "application/vnd.openxmlformats-officedocument.drawingml.chart+xml", _pck.Compression); StreamWriter streamChart = new StreamWriter(chartPart.GetStream(FileMode.Create, FileAccess.Write)); streamChart.Write(xml); streamChart.Close(); //Now create the new relationship to the copied chart xml var prevRelID = draw.TopNode.SelectSingleNode("xdr:graphicFrame/a:graphic/a:graphicData/c:chart/@r:id", Copy.Drawings.NameSpaceManager).Value; var rel = part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(uriDraw, UriChart), TargetMode.Internal, ExcelPackage.schemaRelationships + "/chart", PackagePartForMono.NextRelationshipID); XmlAttribute relAtt = drawXml.SelectSingleNode(string.Format("//c:chart/@r:id[.='{0}']", prevRelID), Copy.Drawings.NameSpaceManager) as XmlAttribute; relAtt.Value = rel.Id; } else if (draw is ExcelPicture) { ExcelPicture pic = draw as ExcelPicture; var uri = pic.UriPic; if (!workSheet.Workbook._package.Package.PartExists(uri)) { var picPart = workSheet.Workbook._package.Package.CreatePart(uri, pic.ContentType, CompressionOption.NotCompressed); pic.Image.Save(picPart.GetStream(FileMode.Create, FileAccess.Write), pic.ImageFormat); } var prevRelID = draw.TopNode.SelectSingleNode("xdr:pic/xdr:blipFill/a:blip/@r:embed", Copy.Drawings.NameSpaceManager).Value; var rel = part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(workSheet.WorksheetUri, uri), TargetMode.Internal, ExcelPackage.schemaRelationships + "/image", PackagePartForMono.NextRelationshipID); XmlAttribute relAtt = drawXml.SelectSingleNode(string.Format("//xdr:pic/xdr:blipFill/a:blip/@r:embed[.='{0}']", prevRelID), Copy.Drawings.NameSpaceManager) as XmlAttribute; relAtt.Value = rel.Id; } } //rewrite the drawing xml with the new relID's streamDrawing = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamDrawing.Write(drawXml.OuterXml); streamDrawing.Close(); //} }
private void CopyTable(ExcelWorksheet Copy, ExcelWorksheet added) { string prevName = ""; //First copy the table XML foreach (var tbl in Copy.Tables) { string xml = tbl.TableXml.OuterXml; int Id = _pck.Workbook._nextTableID++; string name; if (prevName == "") { name = Copy.Tables.GetNewTableName(); } else { int ix = int.Parse(prevName.Substring(5)) + 1; name = string.Format("Table{0}", ix); while (_pck.Workbook.ExistsPivotTableName(name)) { name = string.Format("Table{0}", ++ix); } } prevName = name; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(xml); xmlDoc.SelectSingleNode("//d:table/@id", tbl.NameSpaceManager).Value = Id.ToString(); xmlDoc.SelectSingleNode("//d:table/@name", tbl.NameSpaceManager).Value = name; xmlDoc.SelectSingleNode("//d:table/@displayName", tbl.NameSpaceManager).Value = name; xml = xmlDoc.OuterXml; var uriTbl = new Uri(string.Format("/xl/tables/table{0}.xml", Id), UriKind.Relative); var part = _pck.Package.CreatePart(uriTbl, "application/vnd.openxmlformats-officedocument.spreadsheetml.table+xml", _pck.Compression); StreamWriter streamTbl = new StreamWriter(part.GetStream(FileMode.Create, FileAccess.Write)); streamTbl.Write(xml); streamTbl.Close(); //create the relationship and add the ID to the worksheet xml. var rel = added.Part.CreateRelationship(PackUriHelperMonoSafe.GetRelativeUri(added.WorksheetUri, uriTbl), TargetMode.Internal, ExcelPackage.schemaRelationships + "/table", PackagePartForMono.NextRelationshipID); if (tbl.RelationshipID == null) { var topNode = added.WorksheetXml.SelectSingleNode("//d:tableParts", tbl.NameSpaceManager); if (topNode == null) { added.CreateNode("d:tableParts"); topNode = added.WorksheetXml.SelectSingleNode("//d:tableParts", tbl.NameSpaceManager); } XmlElement elem = added.WorksheetXml.CreateElement("tablePart", ExcelPackage.schemaMain); topNode.AppendChild(elem); elem.SetAttribute("id", ExcelPackage.schemaRelationships, rel.Id); } else { XmlAttribute relAtt; relAtt = added.WorksheetXml.SelectSingleNode(string.Format("//d:tableParts/d:tablePart/@r:id[.='{0}']", tbl.RelationshipID), tbl.NameSpaceManager) as XmlAttribute; relAtt.Value = rel.Id; } } }