Microsoft.Office.Interop.Excel.WorkbookClass' in Assembly 'Microsoft.Office.Interop.Excel, is not marked as serializable

廉价感情. 提交于 2019-12-11 03:55:32

问题


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
    {

    }
}

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8998836/microsoft-office-interop-excel-workbookclass-in-assembly-microsoft-office-inte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!