Home > DeveloperSection > Forums > how to export data from datagridview to excel in c#.net windows application.
Ankit Singh

Total Post:341

Points:2389
Posted on    January-07-2016 11:47 PM

 C# C#  Excel 
Ratings:


 3 Reply(s)
 2083  View(s)
Rate this:
I want to export data from datagridview to excel in c#.net windows application.how to do this please help me.


aditya kumar Patel

Total Post:254

Points:1810
Posted on    January-07-2016 11:49 PM

I will explain here how to export data 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

 


Modified On Jan-07-2016 11:57:13 PM

aditya kumar Patel

Total Post:254

Points:1810
Posted on    January-08-2016 12:54 PM

Post is removed by the Admin.

Copbel Beldi
Copbel Beldi

Total Post:1

Points:7
Posted on    July-06-2017 5:43 AM

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.


Don't want to miss updates? Please click the below button!

Follow MindStick