Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Export DataGridView to Excel in C#This code example demonstrates how to export data from DataGridView control toExcel docu...
ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;}Step 5: Storing Each row and column value to excel sheetfo...
Complete Source Codeusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Syst...
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)            {                ExcelApp.Cells[1, i] = dataGridView1...
Upcoming SlideShare
Loading in …5
×

Export data gridview to excel in c#

13,537 views

Published on

  • 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

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

×