1

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.

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
ani
  • 111
  • 1
  • 5
  • 14
  • possible duplicate of [Microsoft.Office.Interop.Excel.WorkbookClass' in Assembly 'Microsoft.Office.Interop.Excel, is not marked as serializable](http://stackoverflow.com/questions/8998836/microsoft-office-interop-excel-workbookclass-in-assembly-microsoft-office-inte) – phoog Jan 25 '12 at 07:16
  • if you want to add information to a question you should edit the original question, not ask a new question. – phoog Jan 25 '12 at 07:38
  • 2
    @ani, looking at the other questions you asked on stackoverflow, there are a few points that should be clear for you now, 1) Don't use office COM in asp.net. 2) if you persist, you can't avoid using a temporary file. 3) consider doing this with xml+xslt to give xlsx files, this you can do with out a temp file. 4) no you really can't serlialize COM objects, and even if you would, this will never be a valid .xls(x) file. – Captain Coder Jan 25 '12 at 07:50
  • I have to reiterate what @CaptainCoder said: do not use Office Automation in ASP.NET or any other server application. It's not designed for it, it won't work properly, it will break in ways that are difficult to diagnose, it's not supported, and may even violate your license agreement with Microsoft unless everyone using that functionality of the web site also has an Office license of their own. **Just Don't Do It** – John Saunders Jan 25 '12 at 12:13

1 Answers1

6

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.

Captain Coder
  • 798
  • 5
  • 12