Open Microsoft Excel spreadsheet with ADO.NET

If your spreadsheet is set up to look like a table, you can easily connect and work with it using SQL. Here's a quick sample to get you started.

If your spreadsheet is set up to look like a table, you can easily connect and work with it using SQL. Here's a quick sample to get you started.


If your spreadsheet is set up to look like a table, you can easily connect and
work with it using SQL. Here's a quick sample to get you started.

If you would like a more elaborate example of how to use ADO.NET with Excel including updating data, check out this article:

http://www.eggheadcafe.com/articles/20021221.asp


strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\TEMP\TEST.XLS;Extended Properties=Excel 8.0;";

string sList="myspreadsheetname";


OleDbConnection oConn = new OleDbConnection();

oConn.ConnectionString = strConn;
oConn.Open();

OleDbDataAdapter oCmd = new OleDbDataAdapter("SELECT * FROM [" + sList + "$]", oConn);
DataSet oDS = new DataSet();
oCmd.Fill(oDS);

foreach(DataRow oRow in oDS.Tables[0].Rows)
{
Response.Write("Row: " + oRow["COLUMNNAME"].ToString() + "<br>");
}


if (oConn.State == ConnectionState.Open) { oConn.Close(); }

By Robbe Morris   Popularity  (554 Views)
Picture
Biography - Robbe Morris
Robbe has been a Microsoft MVP in C# since 2004. He is also the co-founder of NullSkull.com which provides .NET articles, book reviews, software reviews, and software download and purchase advice.  Robbe also loves to scuba dive and go deep sea fishing in the Florida Keys or off the coast of Daytona Beach. Microsoft MVP