예제 #1
0
        public override byte[] ReadStream(string name)
        {
            UCOMIStream stream1;
            STATSTG     statstg1;

            if (this.Disposed)
            {
                throw new ObjectDisposedException("UnmanagedStorage");
            }
            OLE_MODE ole_mode1 = OLE_MODE.STGM_SHARE_EXCLUSIVE | OLE_MODE.STGM_READWRITE;

            try
            {
                this.storage.OpenStream(name, IntPtr.Zero, (uint)ole_mode1, 0, out stream1);
            }
            catch (COMException)
            {
                throw new Exception("Provided file is not a valid BIFF8 file. Only XLS files from Excel 97 and on are supported.");
            }
            if (stream1 == null)
            {
                throw new Exception("Can't open OLE2 stream.");
            }
            stream1.Stat(out statstg1, 1);
            byte[]   buffer1 = new byte[statstg1.cbSize];
            GCHandle handle1 = GCHandle.Alloc(buffer1, GCHandleType.Pinned);

            stream1.Read(buffer1, (int)statstg1.cbSize, IntPtr.Zero);
            handle1.Free();
            return(buffer1);
        }
예제 #2
0
        public override void WriteStream(string name, byte[] buffer)
        {
            UCOMIStream stream1;

            if (this.Disposed)
            {
                throw new ObjectDisposedException("UnmanagedStorage");
            }
            OLE_MODE ole_mode1 = OLE_MODE.STGM_SHARE_EXCLUSIVE | OLE_MODE.STGM_READWRITE;

            this.storage.CreateStream(name, (uint)ole_mode1, 0, 0, out stream1);
            if (stream1 == null)
            {
                throw new Exception("Can't create OLE2 stream.");
            }
            GCHandle handle1 = GCHandle.Alloc(buffer, GCHandleType.Pinned);

            stream1.Write(buffer, buffer.Length, IntPtr.Zero);
            handle1.Free();
            stream1.Commit(0);
            Marshal.ReleaseComObject(stream1);
        }