/// <summary> /// Create or read the XML for the workbook. /// </summary> private void CreateWorkbookXml(XmlNamespaceManager namespaceManager) { if (_package.Package.PartExists(WorkbookUri)) { _workbookXml = _package.GetXmlFromUri(WorkbookUri); } else { // create a new workbook part and add to the package Packaging.ZipPackagePart partWorkbook = _package.Package.CreatePart(WorkbookUri, @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", _package.Compression); // create the workbook _workbookXml = new XmlDocument(namespaceManager.NameTable); _workbookXml.PreserveWhitespace = ExcelPackage.preserveWhitespace; // create the workbook element XmlElement wbElem = _workbookXml.CreateElement("workbook", ExcelPackage.schemaMain); // Add the relationships namespace wbElem.SetAttribute("xmlns:r", ExcelPackage.schemaRelationships); _workbookXml.AppendChild(wbElem); // create the bookViews and workbooks element XmlElement bookViews = _workbookXml.CreateElement("bookViews", ExcelPackage.schemaMain); wbElem.AppendChild(bookViews); XmlElement workbookView = _workbookXml.CreateElement("workbookView", ExcelPackage.schemaMain); bookViews.AppendChild(workbookView); // save it to the package StreamWriter stream = new StreamWriter(partWorkbook.GetStream(FileMode.Create, FileAccess.Write)); _workbookXml.Save(stream); //stream.Close(); _package.Package.Flush(); } }
/// <summary> /// Saves the XmlDocument into the package at the specified Uri. /// </summary> /// <param name="uri">The Uri of the component</param> /// <param name="xmlDoc">The XmlDocument to save</param> internal void SavePart(Uri uri, XmlDocument xmlDoc) { Packaging.ZipPackagePart part = this.Package.GetPart(uri); xmlDoc.Save(part.GetStream(FileMode.Create, FileAccess.Write)); }
internal ZipPackage(Stream stream, string tempFolder) { this.tempFolder = tempFolder; bool hasContentTypeXml = false; if (stream == null || stream.Length == 0) { AddNew(); } else { var rels = new Dictionary <string, string>(); stream.Seek(0, SeekOrigin.Begin); using (ZipInputStream zip = new ZipInputStream(stream)) { var e = zip.GetNextEntry(); while (e != null) { if (e.UncompressedSize > 0) { if (e.FileName.Equals("[content_types].xml", StringComparison.InvariantCultureIgnoreCase)) // Conflict OrdinalIgnoreCase { var b = new byte[e.UncompressedSize]; var size = zip.Read(b, 0, (int)e.UncompressedSize); AddContentTypes(Encoding.UTF8.GetString(b)); hasContentTypeXml = true; } else if (e.FileName.Equals("_rels/.rels", StringComparison.OrdinalIgnoreCase)) { var b = new byte[e.UncompressedSize]; var size = zip.Read(b, 0, (int)e.UncompressedSize); ReadRelation(Encoding.UTF8.GetString(b), ""); } else { if (e.FileName.EndsWith(".rels", StringComparison.OrdinalIgnoreCase)) { var b = new byte[e.UncompressedSize]; var size = zip.Read(b, 0, (int)e.UncompressedSize); rels.Add(GetUriKey(e.FileName), Encoding.UTF8.GetString(b)); } else { var part = new ZipPackagePart(this, e); //part.Stream = new MemoryStream(); //part.Stream.Write(b, 0, b.Length); Stream fs = part.GetStream(); ExcelPackage.CopyStream(zip, fs); Parts.Add(GetUriKey(e.FileName), part); } } } else { } e = zip.GetNextEntry(); } foreach (var p in Parts) { string name = Path.GetFileName(p.Key); string extension = Path.GetExtension(p.Key); string relFile = string.Format("{0}_rels/{1}.rels", p.Key.Substring(0, p.Key.Length - name.Length), name); if (rels.ContainsKey(relFile)) { p.Value.ReadRelation(rels[relFile], p.Value.Uri.OriginalString); } if (_contentTypes.ContainsKey(p.Key)) { p.Value.ContentType = _contentTypes[p.Key].Name; } else if (extension.Length > 1 && _contentTypes.ContainsKey(extension.Substring(1))) { p.Value.ContentType = _contentTypes[extension.Substring(1)].Name; } } if (!hasContentTypeXml) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } if (!hasContentTypeXml) { throw (new InvalidDataException("The file is not an valid Package file. If the file is encrypted, please supply the password in the constructor.")); } zip.Close(); zip.Dispose(); } } }