24 August 2015

.xlsx sheet data to datatable in C#

using System.Data.OleDb;

string qry = "Select * from [Sheet1$]";

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + _xlsheet_filepath + ";Extended Properties=\"Excel 12.0;HDR=YES;\";Persist Security Info=False";
OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
excelConnection.Open();

DataTable dt = new DataTable();
OleDbDataAdapter exDA = new OleDbDataAdapter(qry, excelConnection);
exDA.Fill(dt);
excelConnection.Close();

No comments: