The simplest way is to use
ADO.NET[
^] (
OleDb[
^]).
Steps to do:
1. Create
OleDbConnection[
^]
2. Create
OleDbCommand[
^]
3. Execute command to
OleDbReader[
^]
4. Load data to
DataTable[
^]
5. Bind data to DataGridView via
DataSource[
^]
string sFileName = @"D:\MyUsers.xlsx";
string sConStr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES';", sFileName);
using (OleDbConnection connection = new OleDbConnection(sConStr))
{
string sql = "SELECT * FROM [Sheet1$]";
connection.Open();
OleDbCommand command = new OleDbCommand(sql, connection);
OleDbDataReader reader = command.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
this.dataGridView1.DataSource = dt;
reader.Close();
reader.Dispose();
command.Dispose();
}
For further details, please see:
How to: Bind Data to the Windows Forms DataGridView Control | Microsoft Docs[
^]