Example #1
0
        public static void WriteWorkbookFile(XFile workbookFile, List <SheetInfo> sheets, MemoryFolder mFolder)
        {
            MemoryStream stream = new MemoryStream();

            new XmlWriterSettings().Encoding = Encoding.UTF8;
            XmlWriter writer = XmlWriter.Create((Stream)stream, new XmlWriterSettings());

            writer.WriteStartDocument();
            writer.WriteStartElement("workbook", "http://schemas.openxmlformats.org/spreadsheetml/2006/main");
            writer.WriteAttributeString("xmlns", "r", null, "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            writer.WriteStartElement("sheets");
            foreach (SheetInfo info in sheets)
            {
                writer.WriteStartElement("sheet");
                writer.WriteAttributeString("name", info.name);
                writer.WriteAttributeString("sheetId", ((uint)info.sheetID).ToString());
                writer.WriteAttributeString("r", "id", "http://schemas.openxmlformats.org/officeDocument/2006/relationships", info.rID);
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            stream.Seek(0L, (SeekOrigin)SeekOrigin.Begin);
            mFolder.CreateMemoryFile(workbookFile.FileName, (Stream)stream);
        }
Example #2
0
        /// <summary>
        /// Extracts the zip.
        /// </summary>
        /// <param name="zipStream">zip stream</param>
        /// <returns>Instance of MemoryFolder</returns>
        public static MemoryFolder ExtractZip(Stream zipStream)
        {
            if ((zipStream == null) || !zipStream.CanRead)
            {
                return(null);
            }
            MemoryFolder folder  = new MemoryFolder();
            ZipArchive   archive = new ZipArchive(zipStream);

            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                string fullName = entry.FullName;
                using (Stream stream = entry.Open())
                {
                    MemoryStream stream2 = new MemoryStream();
                    int          count   = 0x800;
                    byte[]       buffer  = new byte[count];
                    while (true)
                    {
                        count = stream.Read(buffer, 0, buffer.Length);
                        if (count <= 0)
                        {
                            break;
                        }
                        stream2.Write(buffer, 0, count);
                    }
                    stream2.Seek(0L, (SeekOrigin)SeekOrigin.Begin);
                    folder.CreateMemoryFile(fullName, (Stream)stream2);
                }
            }
            return(folder);
        }
Example #3
0
        public static void SaveContentTypes(XFile file, MemoryFolder mFolder, ExcelFileType workbookType)
        {
            CT_Types types = GetContentTypes(file, mFolder, workbookType);

            if (types != null)
            {
                mFolder.CreateMemoryFile("[Content_Types].xml", CreateStreamFromObject(types, typeof(CT_Types)));
            }
        }
Example #4
0
 private static void SavePackageRelationFiles(XFile xFile, MemoryFolder mFolder)
 {
     if (((xFile != null) && (xFile.RelationFiles != null)) && ((xFile.RelationFiles.Count != 0) && (mFolder != null)))
     {
         string str  = string.IsNullOrEmpty(xFile.FileName) ? "" : Path.GetDirectoryName(xFile.FileName);
         string str2 = string.IsNullOrEmpty(xFile.FileName) ? "" : Path.GetFileName(xFile.FileName);
         Dictionary <string, string[]> htRelationShip = new Dictionary <string, string[]>();
         foreach (string str3 in xFile.RelationFiles.Keys)
         {
             XFile file = xFile.RelationFiles[str3];
             if (file != null)
             {
                 string[] strArray = new string[2];
                 if (!string.IsNullOrWhiteSpace(file.Target))
                 {
                     strArray[0] = file.Target;
                 }
                 else
                 {
                     strArray[0] = file.FileName.Replace('\\', '/');
                 }
                 strArray[1] = file.FileType;
                 if (htRelationShip.ContainsKey(str3))
                 {
                     htRelationShip[str3] = strArray;
                 }
                 else
                 {
                     htRelationShip.Add(str3, strArray);
                 }
             }
             SavePackageRelationFiles(file, mFolder);
         }
         CT_Relationships relationships = ToRelationships(htRelationShip);
         if (relationships != null)
         {
             mFolder.CreateMemoryFile(str + @"\_rels\" + str2 + ".rels", PackageXml.CreateStreamFromObject(relationships, typeof(CT_Relationships)));
         }
     }
 }