Hello dear members,
Using visual c# 2008, I want to read data from excel sheet to visual c# 2008 and view it in a dataGridView?
Hope you can help with that.
Regards,
Ali
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Hirendra SisodiyaPosted Feb 28, 2011, 6:13 AM
DataSet ds= new DataSet();
string ConnectionString = "your ConnectionState string " //@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strfile + ";Extended Properties=Excel 5.0";
OleDbConnection objConn = new OleDbConnection(ConnectionString);
objConn.Open();
String strConString = "SELECT * from [Sheet1$]";
OleDbCommand objCmdSelect = new OleDbCommand(strConString, objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataTable dt = new DataTable();
objAdapter1.Fill(ds);
DataGridView1.DataSource = ds.Tables[0];
objConn.Close();
thanks
Please mark as answer if it helps
Garry GibsonPosted Apr 25, 2014, 5:52 AM
I think this is a pretty straightforward way, you directly read an excel file in C# and use that api to pass the cells values and the formatting's.
Ali AlfarajPosted Mar 7, 2011, 6:18 AM
Ibrahim ErsoyPosted Mar 1, 2011, 3:05 PM
If you are looking for the first one: you need to write an Add-in For Excel using VSTO.
If you are looking for the other one: listen to the guys before me.
Sam HobbsPosted Mar 1, 2011, 2:59 PM
Amruta KirdatPosted Mar 1, 2011, 5:39 AM
try this...
string strConn;
//ConnectionString where your excel file reside
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=D:\\Book1.xls;" +
"Extended Properties=Excel 8.0;";
//You must use the $ after the object you reference in the spreadsheet
//Sheet1 is sheet where you want to search, you can also add where condition in this query for that your should require column Name to check...
OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "ExcelInfo");
DataGrid1.DataSource = myDataSet.Tables["ExcelInfo"].DefaultView;
DataGrid1.DataBind();
Suthish NairPosted Feb 28, 2011, 6:13 AM
Remember to use the provider as below.
For .xls -->
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetFullPath(item) + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
For .xlsx -->
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path.GetFullPath(item) + ";Extended Properties='Excel 12.0 Xml;HDR=Yes;IMEX=1'";
Dorababu MekaPosted Feb 28, 2011, 5:43 AM
Krishna GaradPosted Feb 28, 2011, 5:43 AM
Import Excel Data into GridView & Export to Excel Sheet
it may help you