With the help of Oledb (Object Linking and Embedding Database). OLE DB is Microsoft's strategic low-level application program interface (API) for access to different data sources.
The objects in OLE DB consist mainly of a data source object, a session object, a command object, and a rowset object. An application using OLE DB would use this request sequence:
- Initialize OLE.
- Connect to a data source.
- Issue a command.
- Process the results.
- Release the data source object and uninitialize OLE.
here is a link of previous stackoverflow answer how to use olddb to access data from excel.
How to read data from excel file using c#
Here is Sample Code
string path = @"E:\USERS\MyWorkbook.xlsx";
//Create connection string to Excel work book
string excelConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
OleDbConnection excelCon = new OleDbConnection(excelConString);
excelCon.Open();
DataTable dtsheet = new DataTable();
dtsheet = excelCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow row in dtExcelSheet.Rows)
{
Query = string.Format("Select * from [{0}]", row["TABLE_NAME"].ToString());
//Create OleDbCommand to fetch data from Excel
OleDbCommand cmd = new OleDbCommand(Query, excelCon);
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, excelCon);
excelCon.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
foreach (DataRow dr in Exceldt.Rows)
{
//code to display
}
}