How to populate a datagrid in windows form with the datas from an excel file

Posted by Dinee_432 under C# on 10/13/2009 | Views : 3814 | Status : [Member] | Replies : 6
Hi,

I am new to the Windows application.
I am working in dotnet coding using C#.
Can anyone please help in reading the datas from the Excel file and populate it in a datagrid in windows form.
Send me the detailed answer like where to write the code.

Thanks in advance




Responses

Posted by: Vuyiswamb on: 10/13/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Morning

 public  void export_datagridview_to_excel(DataGridView dgv, string excel_file)

{
int cols;
//open file
StreamWriter wr = new StreamWriter(excel_file);

//determine the number of columns and write columns to file
cols = dgv.Columns.Count;
for (int i = 0; i < cols; i++)
{
wr.Write(dgv.ColumnsIdea.Name.ToString().ToUpper() + "\t");
}

wr.WriteLine();

//write rows to excel file
for (int i = 0; i < (dgv.Rows.Count - 1); i++)
{
for (int j = 0; j < cols; j++)
{
if (dgv.RowsIdea.Cells[j].Value != null)
wr.Write(dgv.RowsIdea.Cells[j].Value + "\t");
else
{
wr.Write("\t");
}
}

wr.WriteLine();
}

//close file
wr.Close();
}


or this

  string strPath = @"C:\Test.xls";

string strXLScon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strPath + ";Extended Properties=Excel 8.0";

OleDbConnection oledbConn = new OleDbConnection(strXLScon);
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;

DataSet ds = new DataSet();

oleda.Fill(ds);

this.dataGridView1.DataSource = ds.Tables[0];
this.dataGridView1.DataBind();

or look at this links

http://www.codeproject.com/KB/office/export.aspx


http://anuraj.wordpress.com/2009/09/23/import-export-data-in-ms-excel-using-c/

Thank you for Posting at DotnetFunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Dinee_432, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dinee_432 on: 10/20/2009 [Member] Starter

Up
0
Down
Hi,
Good morning

The code you have sent is not working. The first code is to write to an Excel File. My requirement is to read from an Excel File and write it to a data grid. I have tried the Second code its showing error as external table is not defined.


Thank u vuyiswamb

Dinee_432, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 10/20/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
So Basically you want to show an Excel File into a gridview ?

Thank you for posting at Dotnetfunda
[Administrator]

Dinee_432, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dinee_432 on: 10/21/2009 [Member] Starter

Up
0
Down
Hi Vuyiswamb

Yes I want to show the Excel file in a datagrid. In my excel file the columns may differ for each file. So the columns in the grid should be generated dynamically.


Thanks

Dinee_432, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 10/22/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Then look at this

http://aspnet.4guysfromrolla.com/articles/031208-1.aspx

Thank you for posting at Dotnetfunda


Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Dinee_432, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Dinee_432 on: 10/23/2009 [Member] Starter

Up
0
Down
Hi

I can able to read from excel files but its not reading the numbers and date from excel file. give me a suggestion for this.


Thanks in advance.

Dinee_432, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response