Sponsored By
MindStick Cleaner
Advertise with Us
Advertisement
Advertise with Us
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog
Report Abuse Form
Reason:    
   

Home >> .Net >> C# >> how will data export to excel in datagridview
Author Post

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.
Author Post

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
Report Abuse Answer
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.


Author Post

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?
 
        {
                                
  }

Author Post

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
Author Post

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
Author Post

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.
Author Post

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.
Author Post

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;
Author Post

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
You got it aken :)
Author Post

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.

 

Author Post

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
Author Post

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.
Author Post

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.

Author Post

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.
Author Post

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
Yes really good one...
Author Post

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,
Author Post

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,
Report Abuse Form
Reason:    
 
Total Online Users: 6074
Advertisement
MindStick DataConverter
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.