Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to import data from an excel sheet and display it on a datagrid in a windows form. Any suggestions on how I go about doing this.
Posted

1 solution

Step 1:
You open a browser on your computing device.

Step 2:
You visit Google.com.

Step 3:
You type what it is you want to know or find.

Step 4:
Click the search button.

Step 5:
Look through the results until you find what you need.

Try doing this first next time, it will save you lots of time and will help advance your skills.
 
Share this answer
 
v3
Comments
P.C Shabangu 15-May-13 11:03am    
I am extremely sorry about that richb.
Richard C Bishop 15-May-13 11:07am    
It's all good P.C. I just wanted you to realize that part of programming is being able to research. I did not mean to offend or upset you if that is the case.
Maciej Los 15-May-13 11:05am    
Beautifull tutorial!
+5!
Richard C Bishop 15-May-13 11:07am    
Thank you sir!
P.C Shabangu 16-May-13 4:16am    
Hi all
Below is the code that I researched and it is working fine. But when I went through it, when you are uploading an excel file the code restricts the sheet name to be "Sheet1" on this line "string query = string.Format("select * from [{0}$]", "Sheet1");". How can I tweek the application to actually accept any sheetname and display. Code is below:

private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
DialogResult dlgresult = dlg.ShowDialog();

if (dlgresult == DialogResult.OK)
{
txtpath.Text = dlg.FileName;
}

}


private void btnload_Click(object sender, EventArgs e)
{
if (System.IO.File.Exists(txtpath.Text))
{
try
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtpath.Text);

string query = string.Format("select * from [{0}$]", "Sheet1");
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataset = new DataSet();
dataAdapter.Fill(dataset);
dgrvInfo.DataSource = dataset.Tables[0];


}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

else
{
MessageBox.Show("Please select a file!", "Fail", MessageBoxButtons.OK, MessageBoxIcon.Error);

}

}



}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900