Hello All,
I am the Beginner in C#. i want to read Excel file. in Excel file read based on the key Vale(key value means one column). Please Help me.
Thanks in Advance..
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
Vasu GadhiyaRank #7,540 · 239 reputationPosted Jul 28, 2014, 12:34 AM EDT
try
{
con.Open();
string query = "select * from [Sheet1$]";
OleDbDataAdapter mycommand = new OleDbDataAdapter(query, con);
DataSet ds = new DataSet();
mycommand.Fill(ds, "Table");
dataGridView1.DataSource = ds.Tables[0];
}catch (Exception ex)
{
MessageBox.Show(ex.Source);
}finally
{
con.Close();
}-------------------------------------
to read by column change query list column name select statement.
like
Select id, name from [Sheet1$]
-------------------------------------
Candy Brown24 reputationPosted Dec 8, 2014, 11:22 PM EST
// Load excel file. ExcelFile file = ExcelFile.Load(@"C:\Sample.xlsx"); // Get first worksheet. ExcelWorksheet firstSheet = file.Worksheets[0]; // Various ways of reading 'B3' cell's value. Console.WriteLine(firstSheet.Cells["B3"].Value); Console.WriteLine(firstSheet.Rows["3"].Cells["B"].Value); Console.WriteLine(firstSheet.Rows[2].Cells[1].Value); Console.WriteLine(firstSheet.Columns["B"].Cells["3"].Value); Console.WriteLine(firstSheet.Columns[1].Cells[2].Value);Laxmikant55 reputationPosted Dec 9, 2014, 2:15 AM EST
see below article which will help you to get excelsheet data in datatable using OleDBConnection
http://dotnetmentors.com/aspnet/display-excel-data-in-web-page.aspx