Microsoft.Office.Interop.Excel.WorkbookClass'在程序集“Microsoft.Office.Interop.Excel”中,未标记为可序列化

发布于 2024-12-28 17:28:44 字数 1274 浏览 0 评论 0原文

序列化异常:程序集“Microsoft.Office.Interop.Excel,Version=11.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c”中的类型“Microsoft.Office.Interop.Excel.WorkbookClass”未标记为可序列化。

我收到上述异常..这是代码

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
    {

    }
}

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
    {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆消瘦 2025-01-04 17:28:44

您无法通过序列化保存 Excel 工作簿。您必须使用其 Save 方法保存工作簿。

您说您将该类标记为可序列化,但您一定标记了错误的类。需要标记为可序列化的类是 WorkbookClass,并且您无法控制该类。

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.

浸婚纱 2025-01-04 17:28:44

并非所有内容都适合使用任何特定序列化引擎进行序列化。由于这个案例涉及 COM 互操作,所以我并不感到惊讶。基本上,您不能这样做 - 您只需使用常规保存方法并写入文件,然后从文件加载 BLOB。

另外,由于您也使用 ASP.NET 对此进行了标记,请注意 ASP.NET 中不支持 Office 互操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文