Export data gridview to excel in c#

13,236 views
13,018 views

Published on

3 Comments
2 Likes
Statistics
Notes
  • can we export data without using loops..
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • very useful , thank u ..
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • super .. its very useful for me..
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
13,236
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
171
Comments
3
Likes
2
Embeds 0
No embeds

No notes for slide

Export data gridview to excel in c#

  1. 1. Export DataGridView to Excel in C#This code example demonstrates how to export data from DataGridView control toExcel document using C#.Step 1: Add a reference to the ‘Microsoft.Office.Interop’ to your project from .NETcomponents.Step 2: Creating Excel ApplicationMicrosoft.Office.Interop.Excel.ApplicationClass ExcelApp = newMicrosoft.Office.Interop.Excel.ApplicationClass();ExcelApp.Application.Workbooks.Add(Type.Missing);Step 3: Change properties of the WorkbookExcelApp.Columns.ColumnWidth = 20;Step 4: Storing header part in Excelfor (int i = 1; i < dataGridView1.Columns.Count + 1; i++){
  2. 2. ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;}Step 5: Storing Each row and column value to excel sheetfor (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 exitExcelApp.ActiveWorkbook.SaveCopyAs("C:test.xls");ExcelApp.ActiveWorkbook.Saved = true;ExcelApp.Quit();Windows Forms Layoutand excel file after exporting
  3. 3. Complete Source Codeusing 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 = newMicrosoft.Office.Interop.Excel.ApplicationClass(); ExcelApp.Application.Workbooks.Add(Type.Missing); // Change properties of the Workbook ExcelApp.Columns.ColumnWidth = 20; // Storing header part in Excel
  4. 4. 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(); } }}Download Source Code

×