I am using Excel Data Reader to read some data in an Entity Framework database
The code below works, but I need further clarification
First of all, IsFirstRowAsColumnNames doesn't seem to work properly, and I should use .Read instead.
I had at first to select a specific leaflet, it had plans that could help with this excelReader.Name is currently pointless if I cannot specifically skip or select the leaf that I originally used. hence the conflict.
It would also be useful to look at the actual column header names to get data, not indexes, such as var name = reader ["applicationname"]. ToString () in the SQL client;
Perhaps the best extension I could use to read in excel data if I cannot achieve the above.
public static void DataLoadAliases(WsiContext context)
{
const string filePath = @"Alias Master.xlsx";
var stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
var excelReader = filePath.Contains(".xlsx")
? ExcelReaderFactory.CreateOpenXmlReader(stream)
: ExcelReaderFactory.CreateBinaryReader(stream);
excelReader.IsFirstRowAsColumnNames = true;
excelReader.Read();
while (excelReader.Read())
{
if (excelReader.Name == "Alias Master")
{
var aliasId = excelReader.GetInt16(0);
var aliasName = excelReader.GetString(1);
if (String.IsNullOrEmpty(aliasName)) continue;
context.Aliases.Add(new ApplicationAlias
{
AliasId = aliasId,
Name = aliasName,
});
}
else
{
excelReader.NextResult();
}
}
excelReader.Close();
context.SaveChanges();
}
source
share