title: Read Excel To Class By Reflection
body: <p>I can't seem to find anything like this, but please link if I've missed it.</p>
  
  <p>I'm trying to import a load of data from an Excel spreadsheet (xlsx).  For each row I want to create a List(MyClass), where MyClass has a key/value pair of properties.  For each column, I want to populate MyClass with Name = [Column Name], Value = [Column Value].</p>
  
  <p>I started using OLEDB to extract the data from the spreadsheet into a DataTable, but I'm not sure if this is the best reader available.  It's possible that the spreadsheet will have upwards of 50,000 rows, so I need to consider speed/performance.</p>
  
  <p>This is all I have so far, I can't think how to go about populating MyClass with the column data.</p>
  
  <pre><code>var fileName = string.Format("C:/HAM.xlsx");
  var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", fileName);
  
  var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1]", connectionString);
  var ds = new DataSet();
  adapter.Fill(ds, "hardcatAssets");
  
  var data = ds.Tables["hardcatAssets"].AsEnumerable();
  
  foreach(var row in data)
  {
      List&lt;MyClass&gt; assets = new List&lt;MyClass&gt;();
      // Do I need another foreach on row.columns?, and then add to assets.
  }
  </code></pre>
  
  <p>MyClass</p>
  
  <pre><code>public class MyClass
  {
      public string AssetCode { get; set; }
  
      public string Barcode { get; set; }
  
      public string Description { get; set; }
  
      public string RFIDTag { get; set; }
  }
  </code></pre>
  
  <p>Excel Sheet</p>
  
  <pre><code>Columns: "Asset Code", "Barcode", "Description", "RFID Tag"
  </code></pre>
  
  <p>I've updated my original question, as I changed my mind on the structure of the object I wanted to store each row into.  I'll post what I did below as an answer.</p>