I can't seem to find anything like this, but please link if I've missed it.
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].
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.
This is all I have so far, I can't think how to go about populating MyClass with the column data.
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<MyClass> assets = new List<MyClass>();
// Do I need another foreach on row.columns?, and then add to assets.
}
MyClass
public class MyClass
{
public string AssetCode { get; set; }
public string Barcode { get; set; }
public string Description { get; set; }
public string RFIDTag { get; set; }
}
Excel Sheet
Columns: "Asset Code", "Barcode", "Description", "RFID Tag"
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.