Victor Mihaita
Victor Mihaita

Reputation: 116

Create and use excel file in c# - Unity

I try to write some data from my Unity3D application into an excel file. I've created the following function:

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);

}

When I try to run the app I get the following error:

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

Can someone tell me why I get this exception? Because I think that my program is written ok.. Thank you.

Upvotes: 1

Views: 5541

Answers (1)

misfitgeek
misfitgeek

Reputation: 105

I normally use Spreadsheet XML. Replacing #ColCount accordingly, #RowCount (be generous by padding row count, +5 or so otherwise you will get an error), #Headers and #Rows with appropriate Rows using a for-loop: i.e

Headers

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

Spreadsheet XML Template

<?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>

Then save as filename.xls. Let the use handle the formatting. No need for extra libraries or having Excel installed.

Happy coding. :)

Upvotes: 1

Related Questions