I would like to know how I can read multiple excel worksheet with different worksheet name in c# and with the used of oledb.
I have this existing way to read multiple sheets (but with fixed worksheet name):
DataSet ds = new DataSet();
var excelConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0", path);
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = excelConnectionString;
var i = 1;
while (i <= 4)
{
string query = "SELECT * FROM [Sheet" + i + "$]";
ds.Clear();
OleDbDataAdapter data = new OleDbDataAdapter(query, connection);
data.Fill(ds);
// other stuff
i = i + 1;
}
This one works. But I'm now in different situation wherein the worksheets names are not fixed, example: Sheet1 is Dog, Sheet2 is Cat Sheet3 is Bird.
Now my concern is how I can loop on those worksheet names with the use of my existing code.
See Question&Answers more detail:os