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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hirendra SisodiyaRank #258 · 9.8K reputationPosted Feb 28, 2011, 1:13 AM EST
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
Krishna GaradRank #191 · 12.8K reputationPosted Feb 28, 2011, 12:43 AM EST
Import Excel Data into GridView & Export to Excel Sheet
it may help you
Dorababu MekaRank #457 · 5.5K reputationPosted Feb 28, 2011, 12:43 AM EST
Suthish NairRank #92 · 22.8K reputationPosted Feb 28, 2011, 1:13 AM EST
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'";
Amruta Kirdat72 reputationPosted Mar 1, 2011, 12:39 AM EST
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();
Sam HobbsRank #107 · 20.5K reputationPosted Mar 1, 2011, 9:59 AM EST
Ibrahim ErsoyRank #106 · 20.6K reputationPosted Mar 1, 2011, 10:05 AM EST
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.
Ali Alfaraj62 reputationPosted Mar 7, 2011, 1:18 AM EST
Garry Gibson20 reputationPosted Apr 25, 2014, 1:52 AM EDT
// Load excel file. var workbook = ExcelFile.Load("Workbook.xlsx"); // Export active worksheet to DataGridView. DataGridViewConverter.ExportToDataGridView( workbook.Worksheets.ActiveWorksheet, dataGridView, new ExportToDataGridViewOptions("A4", 7, 1));