|
|
|
|
|
|
|
|
Home
>>
.Net
>>
C#
>>
how will data export to excel in datagridview
|
|
aken H
Total Post:
66
Member Since:
9/6/2011
Points:
419
|
|
how will data export to excel in datagridview
|
|
Posted On:
9/21/2011 11:40:33 PM
|
Hi all,
I have two question,First:Is how will data export to excel in datagridview.Second:
Is how will data import to database in excel.
Thanks in advance.
|
|
|
|
|
|
|
Rohit Kesharwani
Total Post:
54
Member Since:
8/8/2011
Points:
385
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/22/2011 1:14:40 AM
|
hello aken, Here are the solution of your both the problem:
Export Data from Excel in dataGridView:
Write the below code on the button click or on form load function:
using System.Data.OleDb;
OleDbConnection oledbCon = null; DataTable dt = null; string csExcel = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\rohit\\File.xlsx;Extended Properties=\"Excel 8.0;HDR=Yes;\" "; oledbCon = new OleDbConnection(csExcel); oledbCon.Open(); dt = oledbCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); oledbCon.Close(); String[] excelSheets = new String[dt.Rows.Count]; DataRow row = dt.Rows[0];
oledbCon.Open(); OleDbDataAdapter oledbAdapter = new OleDbDataAdapter("select * from [" + row["TABLE_NAME"].ToString() + "]", oledbCon); oledbCon.Close(); DataSet excelDataSet = new DataSet(); oledbAdapter.Fill(excelDataSet, "student"); DataTable excelTable = excelDataSet.Tables["student"]; dataGridView1.DataSource = excelTable;
Import Data from dataGridView in Excel:
using System.Data.OleDb;
OleDbConnection oledbCon = null; DataTable dt = null; string csExcel = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=d:\\rohit\\File.xlsx;Extended Properties=\"Excel 8.0;HDR=Yes;\" "; oledbCon = new OleDbConnection(csExcel); oledbCon.Open(); dt = oledbCon.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); oledbCon.Close(); String[] excelSheets = new String[dt.Rows.Count]; DataRow row = dt.Rows[0]; oledbCon.Open(); for (int i = 0; i < dataGridView1.Rows.Count-1; i++) { OleDbCommand cmd = new OleDbCommand("insert into [" + row["TABLE_NAME"].ToString() + "] values ('" + dataGridView1.Rows[i].Cells[0].Value.ToString() + "','" + dataGridView1.Rows[i].Cells[1].Value.ToString() + "')", oledbCon); cmd.ExecuteNonQuery(); } oledbCon.Close();
Happy Coding.
|
|
|
|
|
|
|
aken H
Total Post:
66
Member Since:
9/6/2011
Points:
419
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/22/2011 4:04:58 AM
|
hi Rohit Kesharwani,
private void btnExportToExcel_Click(object sender, EventArgs e)
{
List<DataGridViewRow> rows_with_checked_column=new List<DataGridViewRow>();
foreach (DataGridViewRow row in dgv.Rows)
{
if (Convert.ToBoolean(row.Cells[0].Value) == true)
//if value is true then start export to excel.
//how to write code?
{
}
|
|
|
|
|
|
|
Rohit Kesharwani
Total Post:
54
Member Since:
8/8/2011
Points:
385
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/22/2011 5:09:45 AM
|
hi,
If you want that only checked rows will be exported then simply manipulate the above code:
List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
foreach (DataGridViewRow rows in dataGridView1.Rows)
{
if (Convert.ToBoolean(rows.Cells[0].Value) == true)
{
oledbCon.Open();
OleDbCommand cmd = new OleDbCommand("insert into [" + row["TABLE_NAME"].ToString() + "] values ('" + rows.Cells[1].Value.ToString() + "','" + rows.Cells[2].Value.ToString() + "')", oledbCon);
cmd.ExecuteNonQuery();
oledbCon.Close();
}
}
Thanks.
|
|
|
|
Modified On:
9/22/2011 5:14:10 AM
|
|
|
aken H
Total Post:
66
Member Since:
9/6/2011
Points:
419
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/22/2011 10:03:54 AM
|
hi Rohit Kesharwani,
i not used using System.Data.OleDb; i want used using System.Data.SqlClient;
Thank you always help me, I hereby express my sincere thank you.
|
|
|
|
Modified On:
9/22/2011 10:27:44 AM
|
|
|
Awadhendra Tiwari
Total Post:
126
Member Since:
1/19/2011
Points:
893
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/23/2011 12:21:29 AM
|
Hi aken H.. You can make following modification on above code
using System.Data.SqlClient; //import this namespace at top
then replace OleDbCommand by SqlCommand class.
Then above code also work for you.
Thanks.
|
|
|
|
|
|
|
Carl Pieterson
Total Post:
13
Member Since:
9/13/2011
Points:
57
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/23/2011 3:31:48 AM
|
Thanks Rohit. It helps me a lot.
|
|
|
|
|
|
|
Uttam Misra
Total Post:
113
Member Since:
6/2/2010
Points:
818
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/23/2011 4:33:46 AM
|
use namespace: using System.Data.SqlClient;
|
|
|
|
|
|
|
John Smith
Total Post:
51
Member Since:
9/27/2010
Points:
332
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/23/2011 4:37:48 AM
|
|
|
|
|
|
|
|
|
aken H
Total Post:
66
Member Since:
9/6/2011
Points:
419
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/23/2011 11:35:25 AM
|
hi Awadhendra Tiwari,Rohit Kesharwani,
Thank you for your help,Thanks Rohit Kesharwani . he helps me a lot.
again ask a question:
you can provide LINQ learning resources to me,and LINQ in project the integrated application.
Grateful.
|
|
|
|
|
|
|
Awadhendra Tiwari
Total Post:
126
Member Since:
1/19/2011
Points:
893
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/24/2011 12:09:43 AM
|
|
|
|
|
|
|
|
|
Haider M Rizvi
Total Post:
40
Member Since:
6/6/2010
Points:
291
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/24/2011 5:50:17 AM
|
Thanks Awadhendra for the links. It helped me.
|
|
|
|
|
|
|
aken H
Total Post:
66
Member Since:
9/6/2011
Points:
419
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/25/2011 6:53:29 PM
|
Hi all,
thanks Awadhendra Tiwari,It helps me a lot.
Thanks.
|
|
|
|
|
|
|
Amit Singh
Total Post:
104
Member Since:
8/8/2010
Points:
702
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/27/2011 2:50:29 AM
|
Thanks Rohit this code work for me.
|
|
|
|
|
|
|
John Smith
Total Post:
51
Member Since:
9/27/2010
Points:
332
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
9/27/2011 10:27:34 AM
|
|
|
|
|
|
|
|
|
James Smith
Total Post:
48
Member Since:
3/16/2011
Points:
270
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
10/1/2011 10:06:21 AM
|
Hi all.. This is really good. Can any one give me some example of datagridview demo in c#. So i can make a complete program on it.
Thanks,
|
|
|
|
|
|
|
Jenry Hock
Total Post:
18
Member Since:
9/7/2011
Points:
116
|
|
Re: how will data export to excel in datagridview
|
|
Posted On:
10/3/2011 5:46:52 AM
|
Hi Aken..
You can use following link to export data from excel to data-grid view,
http://support.microsoft.com/kb/321686
Thanks,
|
|
|
|
|
|
|
|
|
|
|