2
votes

serialization exception: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.

i am getting the above exception..here is the code

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();           

       //below line im getting the 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
    {

    }
}
2
i have marked the class with [serializable] attribute,but no luck - ani
If you need pointers to save an Excel doc, something went wrong. - Henk Holterman

2 Answers

1
votes

You can't save an Excel workbook with serialization. You must save the workbook using its Save method.

You say you marked the class as serializable, but you must have marked the wrong class. The class that needs to be marked serializable is the WorkbookClass, and you don't have control over that class.

0
votes

Not everything is suitable for serializing with any particular serialization engine. Since this case involves COM interop, I'm especially not surprised. Basically, you can't do that - you'll just have to use the regular save methods and write to a file, and load the BLOB from the file.

Additionaly, since you tagged this with ASP.NET too, note that Office interop is not supported in ASP.NET.