Read Data From Excel Sheet

Posted by Lakhangarg under C# category on | Views : 2748
This application will tell you how to read data from excel sheet.
i am using OLEDB provider for this operation.
Connection String for OLEDB is:

string file = Server.MapPath("UserData.xls"); string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + file + ";
Extended Properties=Excel 8.0;";

Read data from Excel Sheet:
query to fetch the records:
string query = "Select UserID,UserName,Country,State,City from [Sheet1$]";

[Sheet1$] name of the sheet in the Excel workbook.And UserID,UserName,Country,State,City are the name of the column in this sheet.
this query will return all the records from Excel Sheet [Sheet1$].

Code to execute the above query is:

DataSet dsUserData = new DataSet();
using (OleDbConnection Connection = new OleDbConnection(constr))
using (OleDbDataAdapter DataAdapter = new OleDbDataAdapter(query, Connection))
DataAdapter.Fill(dsUserData, "UserData");
DataAdapter.AcceptChangesDuringFill = false;

Comments or Responses

Posted by: Priyagoel_24 on: 7/13/2013 Level:Starter | Status: [Member] | Points: 10
i wait your reply

Login to post response