Export Data From Excel Sheet into Dataset- C#

Procedure To Export Excel Sheet Data into Dataset With the help of OLEDB
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.


Complete Code to Get the Excel Sheet Data
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;

0 comments: