read Excel file using c#

How to read an Excel file with OleDb and a simple SQL query?

This approach is extremely useful when you need to read the
data from an Excel file fast and store the data in a DataTable
for further usage.

using System.Data;
using System.Data.OleDb;

...

String sConnectionString =
"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + [Your Excel File Name Here] + ";" +
"Extended Properties=Excel 8.0;";


OleDbConnection objConn = new OleDbConnection(sConnectionString);

objConn.Open();

OleDbCommand objCmdSelect =new OleDbCommand("SELECT * FROM [Sheet1$]", objConn);

OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

objAdapter1.SelectCommand = objCmdSelect;

DataSet objDataset1 = new DataSet();

objAdapter1.Fill(objDataset1);

objConn.Close();

0 comments :

Post a Comment