C# Archives - Tech Insights https://reactconf.org/category/how-to-export-datagridview-to-excel-file-in-c/ Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Thu, 20 Mar 2014 02:43:00 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 C# Archives - Tech Insights https://reactconf.org/category/how-to-export-datagridview-to-excel-file-in-c/ 32 32 230003556 How to Export DataGridView to Excel file in C# https://reactconf.org/how-to-export-datagridview-to-excel/ https://reactconf.org/how-to-export-datagridview-to-excel/#respond Thu, 20 Mar 2014 02:43:00 +0000 http://www.sqlneed.com/2014/03/20/how-to-export-datagridview-to-excel-file-in-c/ First you need to create New Project and give the Name of the project is ExportDatafromdatagridviewtoExcel. Add the reference in your project, right click on the project and click on …

The post How to Export DataGridView to Excel file in C# appeared first on Tech Insights.

]]>

First you need to create New Project and give the Name of the project is ExportDatafromdatagridviewtoExcel. Add the reference in your project, right click on the project and click on add reference.
Now Add Two reference
Microsoft Office 12.0 object Library
Microsoft Excel 12.0 object Library
Add the command button and dataGridView1 control and copy this code and paste. So please change the datasource name,database name and Table name.
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Data.SqlClient;
usingSystem.IO;
usingExcel=Microsoft.Office.Interop.Excel;
namespaceExportDatafromdatagridviewtoExcel
{
    public partial class ExportToExcel : Form
    {
        stringcon = “Data Source=PCName;Initial Catalog=Northwind;Integrated Security=True”;
        publicExportToExcel()
        {
            InitializeComponent();
        }
        privatevoid ExportToExcel_Load(object sender, EventArgs e)
        {
            stringsql = “Select Employeeid,FirstName,LastName from Employees”;
            SqlConnectioncnn = new SqlConnection(con);
            SqlDataAdaptersd = new SqlDataAdapter(sql, cnn);
            DataSetds = new DataSet();
            cnn.Open();
            sd.Fill(ds, “Employees”);
            cnn.Close();
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = “Employees”;
        }
        privatevoid cmdExport_Click(objectsender, EventArgs e)
        {
            Excel._ApplicationExApp = new Microsoft.Office.Interop.Excel.Application();
            Excel._WorkbookExwrk = ExApp.Workbooks.Add(Type.Missing);
            Excel._WorksheetExwsh = null;
            Exwsh=Exwrk.Sheets[“Sheet1”];
            Exwsh = Exwrk.ActiveSheet;
            Exwsh.Name = “Data Exported From DataGridview”;
        //Header
            for(int k =1; k < dataGridView1.Columns.Count + 1; k++ )
            {
                Exwsh.Cells[1,k]=dataGridView1.Columns[k-1].HeaderText;
            }
         
            for(int i = 0; i < dataGridView1.Rows.Count – 1; i++)
            {
                for(int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    Exwsh.Cells[i + 2, j + 1] =  dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }
            Exwsh.SaveAs(“E:\ExportFromDataGrid”);
            ExApp.Quit();
            MessageBox.Show(“Data Exported From DataGridview Completed..”);
        }
    }
}

The post How to Export DataGridView to Excel file in C# appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-export-datagridview-to-excel/feed/ 0 2220