You could try something like this (note that I like to use a set of fall back connection strings, you might want to add strings that are relevant to you there);
public IEnumerable<sometypeofyours> Load(string filename, string sheetName)
{
foreach (string connectionStringBase in new[]
{
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;",
"Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};Extended Properties=Excel 8.0;"
})
{
try
{
string connectionString = String.Format(connectionStringBase, filename);
OleDbDataAdapter adapter = new OleDbDataAdapter(String.Format("select * from [{0}$]", sheetName), connectionString);
DataSet dataset = new DataSet();
adapter.Fill(dataset, "dummy");
DataTable table = dataset.Tables["dummy"];
IList<sometypeofyours> result = new List<sometypeofyours>();
foreach (System.Data.DataRow row in table.Rows)
{
SomeTypeOfYours item = new SomeTypeOfYours { SomeProperty = row.Get(0) };
result.Add(item);
}
return result;
}
catch (Exception)
{
}
}
throw new ArgumentOutOfRangeException("filename", "File does not contain import data in a known format.");
}
Hope this helps,
Fredrik