How to export DataGridView to excel file

The provided C# program serves as a comprehensive guide on how to export data from a DataGridView to an Excel file.

DataGridView to excel

The program begins by establishing a connection to the database and subsequently loading the data into the DataGridView. Once the data is successfully loaded into the DataGridView, the program proceeds to create a new Excel file and seamlessly transfers the data from the DataGridView to the Excel file. This process ensures the accurate representation and preservation of the data within the Excel file, facilitating further analysis and utilization.

By following this program, developers can effectively export data from a DataGridView to an Excel file, enabling streamlined data management and enhanced compatibility across different platforms and applications.

csharp-excel-datagridview

The initial step entails loading the data from the Product table into a DataGridView, offering a comprehensive view of the table's contents. For further information regarding the specific details of the Product table, please refer to the Database Structure.

Subsequently, a new Excel file is created to serve as the destination for the data. The program seamlessly transfers the data from the DataGridView to the Excel file, ensuring accuracy and integrity during the process. By following these steps, developers can proficiently load data from the Product table into a DataGridView, create a corresponding Excel file, and successfully export the data from the DataGridView to the Excel file, facilitating efficient data management and analysis.

Full Source C#
using System; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection cnn ; string connectionString = null; string sql = null; connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;"; cnn = new SqlConnection(connectionString); cnn.Open(); sql = "SELECT * FROM Product"; SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); DataSet ds = new DataSet(); dscmd.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } private void button2_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int i = 0; int j = 0; for (i = 0; i <= dataGridView1.RowCount - 1; i++) { for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) { DataGridViewCell cell = dataGridView1[j, i]; xlWorkSheet.Cells[i + 1, j + 1] = cell.Value; } } xlWorkBook.SaveAs("csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file c:\\csharp.net-informations.xls"); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); } finally { GC.Collect(); } } } }