Sunday, July 1, 2012

Export DataGridView to Excel in C#

This code example demonstrates how to export data from DataGridView control to Excel document using C#.
Step 1: Add a reference to the ‘Microsoft.Office.Interop’ to your project from .NET components.



Step 2: Creating Excel Application
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
ExcelApp.Application.Workbooks.Add(Type.Missing);

Step 3: Change properties of the Workbook
ExcelApp.Columns.ColumnWidth = 20;

Step 4: Storing header part in Excel
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
    ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}

Step 5: Storing Each row and column value to excel sheet
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
    for (int j = 0; j < dataGridView1.Columns.Count; j++)
    {
        ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
    }
}

Step 6: Save Workbook and exit
ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\test.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();

Windows Forms Layout

and excel file after exporting


Complete Source Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Interop;
 
namespace Export_DataGridView_to_Excel
{
    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
            DataSet ds = new DataSet();
            ds.ReadXml("C:\\Products.xml");
            dataGridView1.DataSource = ds.Tables[0];
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            ExcelApp.Application.Workbooks.Add(Type.Missing);
 
            // Change properties of the Workbook 
            ExcelApp.Columns.ColumnWidth = 20;
 
            // Storing header part in Excel
            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }
 
            // Storing Each row and column value to excel sheet
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                    ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
            }
 
            ExcelApp.ActiveWorkbook.SaveCopyAs("C:\\test.xls");
            ExcelApp.ActiveWorkbook.Saved = true;
            ExcelApp.Quit();
        }
    }
}

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

Related Posts:

  • Mathematical Functions Mathematical Functions Standard ANSI SQL-92 supports the following first four basic arithmetic operators: … Read More
  • SQL LEFT JOIN Keyword SQL LEFT JOIN Keyword The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matches in the right table (t… Read More
  • sample code for file upload in asp.net Here we describe how to upload file ,image, media in asp.net and show in repeate control : MediaBrowser.aspx <%@ Page Language="C#" AutoEventWire… Read More
  • Sample code for ajax update panel in asp.net Here we call ajax in asp.net using script manager update panel. by using update panel we can update particular portion of page. Sample code as below … Read More
  • Array In computer memory every byte is an array element. Abstractions translate these bytes into objects and give them meaning. Arrays are a foundational t… Read More

0 comments:

Post a Comment