---
title: "how to export data from datagridview to excel in c#.net windows application."  
description: "how to export data from datagridview to excel in c#.net windows application."  
author: "Anonymous User"  
published: 2016-01-07  
updated: 2018-08-13  
canonical: https://www.mindstick.com/forum/33848/how-to-export-data-from-datagridview-to-excel-in-c-sharp-dot-net-windows-application  
category: "c#"  
tags: ["c#", "excel"]  
reading_time: 2 minutes  

---

# how to export data from datagridview to excel in c#.net windows application.

I want to [export data](https://www.mindstick.com/forum/382/how-to-import-and-export-data-in-sql-server-from-different-file-format) from [datagridview](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) to [excel](https://www.mindstick.com/articles/12937/how-to-import-excel-data-in-sql-server-2014) in [c#.net](https://www.mindstick.com/forum/150/how-to-check-a-uploaded-file-type-using-asp-dot-net-with-c-sharp-dot-net) [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) application.how to do this please help me.

## Replies

### Reply by Ron Debay

Post is removed by the Admin.

### Reply by Copbel Beldi

There are many options for exporting Excel file. One of them is to use an Excel library like EasyXLS.

## The code is below:

```
<code>ExcelDocument workbook = new ExcelDocument(1);            ExcelWorksheet worksheet = (ExcelWorksheet)workbook.easy_getSheetAt(0);ExcelTable table = worksheet.easy_getExcelTable();for (int column = 0; column < dataGridView.Columns.Count; column++){    table.easy_getCell(0, column).setValue(dataGridView.Columns[column].HeaderText);}for (int row = 0; row < dataGridView.Rows.Count-1; row++){    for (int column = 0; column < dataGridView.Columns.Count; column++)    {        xlsTable.easy_getCell(row+1, column).setValue(dataGridView.Rows[row].Cells[column].Value.ToString());     }}workbook.easy_WriteXLSXFile("file.xlsx");</code>
```

For details about exporting other items like colors, fonts, see export datagridview to Excel in C# from EasyXLS website.\

### Reply by Anonymous User

Post is removed by the Admin.

### Reply by Anonymous User

I will explain here how to [export](https://www.mindstick.com/articles/12966/a-guide-to-export-pallets) [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from datagridview to excel in c# windows application.\
Show below Code and excel formate.

```
using System;using System.Windows.Forms;using System.IO;using System.Data;using System.Data.OleDb;using Excel = Microsoft.Office.Interop.Excel; namespace export_Excel{    public partial class Form1 : Form    {                public Form1()        {            InitializeComponent();        }                    private void Form1_Load(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            Excel.Application xelApp;            Excel.Workbook xelWorkBook;            Excel.Worksheet xelWorkSheet;            object misingValue = System.Reflection.Missing.Value;            xelApp = new Excel.Application();            xelWorkBook = xelApp.Workbooks.Add(misingValue);            xelWorkSheet = (Excel.Worksheet)xelWorkBook.Worksheets.get_Item(1);            int i = 0;            int j = 0;            for (i = 0; i <= GridView1.RowCount - 1; i++)            {                for (j = 0; j <= GridView1.ColumnCount - 1; j++)                {                    DataGridViewCell cell = GridView1[j, i];                    xelWorkSheet.Cells[i + 1, j + 1] = cell.Value;                }            }            xelWorkBook.SaveAs("Employee.xls", Excel.XlFileFormat.xlWorkbookNormal, misingValue, misingValue, misingValue, misingValue, Excel.XlSaveAsAccessMode.xlExclusive, misingValue, misingValue, misingValue, misingValue, misingValue);            xelWorkBook.Close(true, misingValue, misingValue);            xelApp.Quit();            Objectreleased(xelWorkSheet);            Objectreleased(xelWorkBook);            Objectreleased(xelApp);            MessageBox.Show("Excel file created , you can see the file E:\\Employee.xls");        }        private void Objectreleased(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();            }        }        }}
```

```

```

## Result

```

```


---

Original Source: https://www.mindstick.com/forum/33848/how-to-export-data-from-datagridview-to-excel-in-c-sharp-dot-net-windows-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
