How to serialize the com or excel objects to binary format so that i can store it in memorystream in asp.net

4.2k views Asked by At

Here is the sample code...but i am getting the serializationException.

xlbook is the object and i want to save this object to memorystream.

unsafe public void Save(IStream stream, bool clearDirty, Excel.Workbook xlbook)
{
    try
    {
        //if (stream == null)
        //{
        //    return;
        //}
        //object data = xlbook;
        if (xlbook == null)
        {
            return;
        }
        // convert data to byteArray   


        MemoryStream memoryStream = new MemoryStream();
        BinaryFormatter binaryFormatter = new BinaryFormatter();
        //AppDomain currentDomain = AppDomain.CurrentDomain;                        
        //currentDomain.AssemblyResolve += new ResolveEventHandler(currentDomain_AssemblyResolve); 


  //here im getting exception.                        
        binaryFormatter.Serialize(memoryStream, xlbook);            
        byte[] bytes = memoryStream.ToArray();
        memoryStream.Close();
        //get memory pointer
        int cb;
        int* pcb = &cb;
        //save data
        byte[] arrayLen = BitConverter.GetBytes(bytes.Length);
        stream.Write(arrayLen, arrayLen.Length, new IntPtr(pcb));
        stream.Write(bytes, bytes.Length, new IntPtr(pcb));
        //currentDomain.AssemblyResolve -= new ResolveEventHandler(currentDomain_AssemblyResolve);
    }
    catch
    {

    }
}

exception i am getting is...

Type 'Microsoft.Office.Interop.Excel.WorkbookClass' in Assembly 'Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable.

1

There are 1 answers

0
Captain Coder On

For the .Net serializer to work, the objects it is to serialize must have a type marked serializable. In this case the WorkbookClass is not marked as such. You can probably get aroud this by either making a wrapper for the WorkbookClass and implement the ISerializable, or you can make a formatter/formatting surogates to do the work for you. But any solution you take is bound to fail.

Why? First of all the COM objects internal structure is not known, only it's interface. They are mostly unmanaged. .Net simply puts a nice wrapper on top of it. So when simply writing all its bytes to a stream, you might (read: will) serialize unmanaged pointers. When deserializing them again, they will not point to anything, or to something wrong. And since they are unmanaged, there is no way to figure out that they are pointers (and not data), nor what they point to. (Or you would have to dig very deep and figure out its binary format at runtime.) Also from what I remember of the office COM objects, they will mostly spawn their own office process, and communicate with that, so you could even end up serializing a handle to a process. All in all, bound to fail.

Probably the WorkbookClass (or it's parent/container) has a .Save()-like method, you should use that and use a FileStream to wherever you saved the data.