Пример #1
0
        internal static bool CreateMultiStreamFile(Stream[] sources, string[] streamNames, string clsId, string author, string title, string comments, Stream output, bool forceInMemory)
        {
            OLEStructuredStorage.UCOMIStorage   storage        = null;
            OLEStructuredStorage.UCOMILockBytes uCOMILockBytes = null;
            IStream stream = null;
            string  text   = null;
            Guid    clsid  = new Guid(clsId);
            long    num    = 0L;
            long    num2   = 0L;

            for (int i = 0; i < sources.Length; i++)
            {
                num2 = Math.Max(sources[i].Length, num2);
                num += sources[i].Length;
            }
            int num3 = Math.Min(1048576, (int)num2 + 512);

            try
            {
                int grfMode  = 134221842;
                int grfMode2 = 18;
                text = Path.GetTempPath() + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture) + ".doc.tmp";
                if (OLEStructuredStorage.StgCreateDocfile(text, grfMode, 0, out storage) != 0)
                {
                    return(false);
                }
                byte[] array = new byte[num3];
                for (int j = 0; j < streamNames.Length; j++)
                {
                    sources[j].Seek(0L, SeekOrigin.Begin);
                    storage.CreateStream(streamNames[j], grfMode2, 0, 0, out stream);
                    if (stream != null)
                    {
                        IntPtr pcbWritten = default(IntPtr);
                        int    num4       = 0;
                        while ((num4 = sources[j].Read(array, 0, num3)) > 0)
                        {
                            stream.Write(array, num4, pcbWritten);
                        }
                        sources[j] = null;
                    }
                    Marshal.ReleaseComObject(stream);
                    stream = null;
                }
                OLEStructuredStorage.IPropertySetStorage propSetStg      = null;
                OLEStructuredStorage.IPropertyStorage    propertyStorage = null;
                OLEStructuredStorage.StgCreatePropSetStg(storage, 0, out propSetStg);
                Guid rfmtid = new Guid("{F29F85E0-4FF9-1068-AB91-08002B27B3D9}");
                propSetStg.Create(ref rfmtid, IntPtr.Zero, 0, grfMode2, out propertyStorage);
                WriteProperty(propertyStorage, 2, title);
                WriteProperty(propertyStorage, 4, author);
                WriteProperty(propertyStorage, 6, comments);
                Marshal.ReleaseComObject(propertyStorage);
                Marshal.ReleaseComObject(propSetStg);
                storage.SetClass(ref clsid);
                storage.Commit(0);
                Marshal.ReleaseComObject(storage);
                storage = null;
                using (Stream stream2 = File.OpenRead(text))
                {
                    int num5 = 0;
                    while ((num5 = stream2.Read(array, 0, array.Length)) > 0)
                    {
                        output.Write(array, 0, num5);
                    }
                }
                array = null;
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (!string.IsNullOrEmpty(text) && File.Exists(text))
                {
                    File.Delete(text);
                }
                if (stream != null)
                {
                    Marshal.ReleaseComObject(stream);
                    stream = null;
                }
                if (storage != null)
                {
                    Marshal.ReleaseComObject(storage);
                    storage = null;
                }
                if (uCOMILockBytes != null)
                {
                    Marshal.ReleaseComObject(uCOMILockBytes);
                    uCOMILockBytes = null;
                }
            }
        }
Пример #2
0
        public static bool CreateSingleStreamFile(Stream source, string streamName, string clsId, Stream output, bool forceInMemory)
        {
            OLEStructuredStorage.UCOMIStorage   storage        = null;
            OLEStructuredStorage.UCOMILockBytes uCOMILockBytes = null;
            IStream stream = null;
            string  text   = null;
            Guid    clsid  = new Guid(clsId);
            int     num    = Math.Min(1048576, (int)source.Length + 512);

            try
            {
                int grfMode  = 134221842;
                int grfMode2 = 18;
                text = Path.GetTempPath() + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture) + ".xls.tmp";
                if (OLEStructuredStorage.StgCreateDocfile(text, grfMode, 0, out storage) != 0)
                {
                    return(false);
                }
                byte[] array = new byte[num];
                storage.CreateStream(streamName, grfMode2, 0, 0, out stream);
                if (stream != null)
                {
                    IntPtr pcbWritten = default(IntPtr);
                    int    num2       = 0;
                    while ((num2 = source.Read(array, 0, array.Length)) > 0)
                    {
                        stream.Write(array, num2, pcbWritten);
                    }
                    source = null;
                }
                Marshal.ReleaseComObject(stream);
                stream = null;
                storage.SetClass(ref clsid);
                storage.Commit(0);
                Marshal.ReleaseComObject(storage);
                storage = null;
                using (Stream stream2 = File.OpenRead(text))
                {
                    int num3 = 0;
                    while ((num3 = stream2.Read(array, 0, array.Length)) > 0)
                    {
                        output.Write(array, 0, num3);
                    }
                }
                array = null;
                return(true);
            }
            finally
            {
                if (!string.IsNullOrEmpty(text) && File.Exists(text))
                {
                    File.Delete(text);
                }
                if (stream != null)
                {
                    Marshal.ReleaseComObject(stream);
                    stream = null;
                }
                if (storage != null)
                {
                    Marshal.ReleaseComObject(storage);
                    storage = null;
                }
                if (uCOMILockBytes != null)
                {
                    Marshal.ReleaseComObject(uCOMILockBytes);
                    uCOMILockBytes = null;
                }
            }
        }