private static bool UpdateFileList(List <object> types, XFile xFile, MemoryFolder mFolder, ExcelFileType workbookType) { if (((types == null) || (xFile == null)) || (mFolder == null)) { return(false); } bool flag = true; if (!string.IsNullOrEmpty(xFile.FileName)) { string str = ConvertRelationshipType2ContentType(xFile.FileType, workbookType); if (!string.IsNullOrEmpty(str)) { CT_Override @override = new CT_Override { PartName = xFile.FileName.Replace('\\', '/'), ContentType = str }; if (new List <string> { "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml", "application/vnd.ms-office.drawingml.diagramDrawing+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml", "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml", "application/vnd.ms-excel.controlproperties+xml", "application/vnd.openxmlformats-officedocument.vmlDrawing" }.Contains(@override.ContentType)) { @override.PartName = xFile.Target.Replace("..", "/xl"); } bool flag2 = false; foreach (object obj2 in types) { if (obj2 is CT_Override) { CT_Override override2 = obj2 as CT_Override; if ((override2.ContentType == @override.ContentType) && (override2.PartName == @override.PartName)) { flag2 = true; break; } } } if (!flag2) { types.Add(@override); } } } if ((xFile.RelationFiles != null) && (xFile.RelationFiles.Count > 0)) { foreach (XFile file in xFile.RelationFiles.Values) { if (file != null) { flag = flag && UpdateFileList(types, file, mFolder, workbookType); } } } return(flag); }
public static Stream CreateStreamFromObject(object obj, Type type) { if (obj == null) { return(null); } Stream stream = null; if (obj is CT_Relationships) { CT_Relationships relationships = obj as CT_Relationships; stream = (Stream) new MemoryStream(); new XmlWriterSettings().Encoding = Encoding.UTF8; XmlWriter @this = XmlWriter.Create(stream, new XmlWriterSettings()); using (@this.WriteDocument(true)) { using (@this.WriteElement("Relationships", "http://schemas.openxmlformats.org/package/2006/relationships")) { foreach (Relationship relationship in relationships.Relationship) { using (@this.WriteElement("Relationship")) { @this.WriteAttributeString("Id", relationship.Id); @this.WriteAttributeString("Type", relationship.Type); @this.WriteAttributeString("Target", relationship.Target.Trim(new char[] { '\n', '\b', ' ' })); if (!string.IsNullOrWhiteSpace(relationship.TargetMode)) { @this.WriteAttributeString("TargetMode", relationship.TargetMode); } } } } } @this.Flush(); stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin); return(stream); } if (obj is CT_Types) { CT_Types types = obj as CT_Types; stream = (Stream) new MemoryStream(); new XmlWriterSettings().Encoding = Encoding.UTF8; XmlWriter writer2 = XmlWriter.Create(stream, new XmlWriterSettings()); using (writer2.WriteDocument(true)) { using (writer2.WriteElement("Types", "http://schemas.openxmlformats.org/package/2006/content-types")) { foreach (object obj2 in types.Items) { if (obj2 is CT_Default) { CT_Default default2 = obj2 as CT_Default; using (writer2.WriteElement("Default")) { writer2.WriteAttributeString("ContentType", default2.ContentType); writer2.WriteAttributeString("Extension", default2.Extension); continue; } } if (obj2 is CT_Override) { CT_Override @override = obj2 as CT_Override; using (writer2.WriteElement("Override")) { writer2.WriteAttributeString("ContentType", @override.ContentType); writer2.WriteAttributeString("PartName", @override.PartName); } } } } } writer2.Flush(); stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin); } return(stream); }