我嘗試從Unity3D應用程序將一些數據寫入excel文件。我創建了以下功能:在c#中創建和使用excel文件 - Unity
void WriteToExcel(int i, decimal time, decimal speed, int hr) {
string file = "newdoc.xls";
Workbook workbook = new Workbook();
Worksheet worksheet = new Worksheet("First Sheet");
worksheet.Cells[i, 0] = new Cell(i);
worksheet.Cells[i, 1] = new Cell(time);
worksheet.Cells[i, 2] = new Cell(speed);
worksheet.Cells[i, 3] = new Cell(hr);
worksheet.Cells.ColumnWidth[0, 1] = 3000;
workbook.Worksheets.Add(worksheet);
workbook.Save(file);
}
當我嘗試運行應用程序,我得到以下錯誤:
Unhandled Exception: System.TypeLoadException: Could not load type
'ExcelLibrary.SpreadSheet.CellStyle' from assembly 'ExcelLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.at (wrapper managed-to-native) System.Reflection.MonoMethodInfo:get_method_info (intptr,System.Reflection.MonoMethodInfo&)
at System.Reflection.MonoMethodInfo.GetMethodInfo (IntPtr handle) [0x00000] in :0
at System.Reflection.MonoMethodInfo.GetAttributes (IntPtr handle) [0x00000] in :0
at System.Reflection.MonoMethod.get_Attributes() [0x00000] in :0
at System.Reflection.MethodBase.get_IsVirtual() [0x00000] in :0
at Mono.CSharp.MemberCache.AddMethods (BindingFlags bf, System.Type type) [0x00000] in :0
at Mono.CSharp.MemberCache.AddMethods (System.Type type) [0x00000] in :0
at Mono.CSharp.MemberCache..ctor (IMemberContainer container) [0x00000] in :0
at Mono.CSharp.TypeHandle..ctor (System.Type type) [0x00000] in :0
at Mono.CSharp.TypeHandle.GetTypeHandle (System.Type t) [0x00000] in :0
at Mono.CSharp.TypeHandle.GetMemberCache (System.Type t) [0x00000] in :0
at Mono.CSharp.TypeManager.MemberLookup_FindMembers (System.Type t, MemberTypes mt, BindingFlags bf, System.String name, System.Boolean& used_cache) [0x00000] in :0
at Mono.CSharp.TypeManager.RealMemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in :0
at Mono.CSharp.TypeManager.MemberLookup (System.Type invocation_type, System.Type qualifier_type, System.Type queried_type, MemberTypes mt, BindingFlags original_bf, System.String name, IList almost_match) [0x00000] in :0
at Mono.CSharp.IndexerAccess+Indexers.GetIndexersForTypeOrInterface (System.Type caller_type, System.Type lookup_type) [0x00000] in :0
at Mono.CSharp.IndexerAccess+Indexers.GetIndexersForType (System.Type caller_type, System.Type lookup_type) [0x00000] in :0
at Mono.CSharp.IndexerAccess.ResolveAccessor (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression right_side) [0x00000] in :0
at Mono.CSharp.IndexerAccess.DoResolveLValue (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression right_side) [0x00000] in :0
at Mono.CSharp.ElementAccess.DoResolveLValue (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression right_side) [0x00000] in :0
at Mono.CSharp.Expression.ResolveLValue (Mono.CSharp.ResolveContext ec, Mono.CSharp.Expression right_side) [0x00000] in :0
at Mono.CSharp.Assign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0
at Mono.CSharp.SimpleAssign.DoResolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0
at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec, ResolveFlags flags) [0x00000] in :0
at Mono.CSharp.Expression.Resolve (Mono.CSharp.ResolveContext ec) [0x00000] in :0
at Mono.CSharp.ExpressionStatement.ResolveStatement (Mono.CSharp.BlockContext ec) [0x00000] in :0
at Mono.CSharp.StatementExpression.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in :0
at Mono.CSharp.Block.Resolve (Mono.CSharp.BlockContext ec) [0x00000] in :0
at Mono.CSharp.ToplevelBlock.Resolve (Mono.CSharp.FlowBranching parent, Mono.CSharp.BlockContext rc, Mono.CSharp.ParametersCompiled ip, IMethodData md) [0x00000] in :0
誰能告訴我,爲什麼我得到這個例外?因爲我認爲我的程序寫得很好。 謝謝。
您可能會考慮使用Microsoft Office Interop庫以外的庫。 EPPlus,NPOI或Open Office XML SDK將是不錯的選擇。 – mason
我相信你會得到這個異常,因爲你使用的DLL文件與Mono不兼容。您需要找到要使用的Mono兼容庫,不是Office Interop庫,不確定關於EPPlus,但是Open Office XML SDK可能是。 –