Fetch data from Excel

The below code explains how you can fetch data from EXcel to a data adapter . Here I have used a OLEDB Connection

public void GetExcelData(string strFilename)
dataset dsExcelData;
string sCurrentSheet = "[Sheet1$]";
string strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sFilename + ";Extended Properties='Excel 8.0;HDR=Yes'";
OleDbConnection oConnection = new OleDbConnection(strConnectionString);
OleDbCommand oCommand = new OleDbCommand("Select [MAKE BOX], [L],[W],[D],[ID],[SEQ],[PART],[PARTDESCRIPTION] from " + sCurrentSheet, oConnection);
OleDbDataAdapter oDataAdapter = new OleDbDataAdapter(oCommand);
oDataAdapter.Fill(dsExcelData);// dataset

catch (Exception)


// binding to a datagridview control

BindingSource bsDataBind = new BindingSource();
bsDataBind.DataSource = dsExcelData;
datagridview1.DataSource = bsDataBind;

or you can directly bind

datagridview1.datasourse= dsExcelData;


Author: Filip20 Aug 2009 Member Level: Silver   Points : 0

Nice code but OLEDB is slow. Try using GemBox . It's much faster then OLEDB and easier to use.

Guest Author: sheik24 May 2012

Its very useful to us..

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: