Import data from Excel to Gridview on Window form

Introduction

This article will help you understand how to Import data from excel to gridview by use OdbcDataAdapter

Code detail for button Click

openFileDialog.Filter = "Excel 2007 (*.xlsx)|*.xlsx|Excel 2003 (*.xls)|*.xls";
openFileDialog.Title = "Open file Excel";
openFileDialog.InitialDirectory = @"C:\";
openFileDialog.ShowHelp = true;
if (openFileDialog.ShowDialog()== DialogResult.OK)
{
string strConnection;
strConnection = @"Dsn=Excel Files;dbq=" + openFileDialog.FileName+ ";driverid=1046;maxbuffersize=2048;pagetimeout=5";
OdbcDataAdapter Adapter = new OdbcDataAdapter("SELECT* FROM [" + "newSheet1"  +"$]",strConnection);
DataSet ds =new DataSet();
Adapter.Fill(ds, "ExcelInfo");

dataGridView.DataSource = ds;
dataGridView.DataMember = "ExcelInfo";
}



Note : I have tried it on window form . Click here to download code demo .
Good luck

No comments:

Post a Comment