2015-04-27 29 views
0

我嘗試從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

誰能告訴我,爲什麼我得到這個例外?因爲我認爲我的程序寫得很好。 謝謝。

+3

您可能會考慮使用Microsoft Office Interop庫以外的庫。 EPPlus,NPOI或Open Office XML SDK將是不錯的選擇。 – mason

+1

我相信你會得到這個異常,因爲你使用的DLL文件與Mono不兼容。您需要找到要使用的Mono兼容庫,不是Office Interop庫,不確定關於EPPlus,但是Open Office XML SDK可能是。 –

回答

0

我通常使用Spreadsheet XML。更換#ColCount因此,#RowCount(被填充的行數,+ 5左右,否則你會得到一個錯誤大方),#Headers並使用一個for循環相應的行#Rows:即

頁眉

<Row> 
<Cell><Data ss:Type="String">s</Data></Cell> 
<Cell><Data ss:Type="String">d</Data></Cell> 
<Cell><Data ss:Type="String">f</Data></Cell> 
<Cell><Data ss:Type="String">h</Data></Cell> 
<Cell><Data ss:Type="String">j</Data></Cell> 
<Cell><Data ss:Type="String">j</Data></Cell> 
</Row> 

RowData

<Row> 
<Cell><Data ss:Type="Number">5</Data></Cell> 
<Cell><Data ss:Type="Number">4</Data></Cell> 
<Cell><Data ss:Type="Number">3</Data></Cell> 
<Cell><Data ss:Type="Number">2</Data></Cell> 
<Cell><Data ss:Type="Number">1</Data></Cell> 
<Cell><Data ss:Type="Number">0</Data></Cell> 
</Row> 

電子表格XML模板

<?xml version="1.0"?> 
<?mso-application progid="Excel.Sheet"?> 
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:x="urn:schemas-microsoft-com:office:excel" 
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" 
xmlns:html="http://www.w3.org/TR/REC-html40"> 
<Styles> 
    <Style ss:ID="Default" ss:Name="Normal"> 
    <Alignment ss:Vertical="Bottom"/> 
    <Borders/> 
    <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/> 
    <Interior/> 
    <NumberFormat/> 
    <Protection/> 
    </Style> 
</Styles> 
<Worksheet ss:Name="Sheet1"> 
    <Table ss:ExpandedColumnCount="#ColCount" ss:ExpandedRowCount="#RowCount" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultRowHeight="15"> 
    <#Headers> 
    <#RowData> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.3"/> 
    <Footer x:Margin="0.3"/> 
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
    </PageSetup> 
    <Selected/> 
    <Panes> 
    <Pane> 
    <Number>3</Number> 
    <RangeSelection>R1C1:R2C6</RangeSelection> 
    </Pane> 
    </Panes> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
<Worksheet ss:Name="Sheet2"> 
    <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultRowHeight="15"> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.3"/> 
    <Footer x:Margin="0.3"/> 
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
    </PageSetup> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
<Worksheet ss:Name="Sheet3"> 
    <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1" 
    x:FullRows="1" ss:DefaultRowHeight="15"> 
    </Table> 
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel"> 
    <PageSetup> 
    <Header x:Margin="0.3"/> 
    <Footer x:Margin="0.3"/> 
    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/> 
    </PageSetup> 
    <ProtectObjects>False</ProtectObjects> 
    <ProtectScenarios>False</ProtectScenarios> 
    </WorksheetOptions> 
</Worksheet> 
</Workbook> 

然後保存作爲filename.xls。讓用處理格式。無需額外的庫或安裝Excel。

快樂編碼。 :)

相關問題