Export Data From Excel Sheet into Dataset

Connection String For Excel & Query to Fetch The Records

string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties=Excel 8.0;";

Select Column1,Column2,Column3,Column4 from [Sheet1$] (Sheet1 is the name of the sheet in the Excel file from where you want to get the records.)


string file = "Physical path of the File"; (Like: D:\\Amt.xls)
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";Extended Properties=Excel 8.0;";

string query = "Select Column1,Column2,Column3,Column4 from [Sheet1$]";
DataSet dataSet = new DataSet();
using (OleDbConnection Connection = new OleDbConnection(constr))
{
using (OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, Connection))
{
DataAdapter.Fill(dataSet, "DataSetName");
DataAdapter.AcceptChangesDuringFill = false;


Comments

No responses found. Be the first to comment...


  • 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:
    Email: