问题
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