Export data gridview to excel in c#
Upcoming SlideShare
Loading in...5
×
 

Export data gridview to excel in c#

on

  • 13,329 views

 

Statistics

Views

Total Views
13,329
Views on SlideShare
13,329
Embed Views
0

Actions

Likes
2
Downloads
156
Comments
3

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • can we export data without using loops..
    Are you sure you want to
    Your message goes here
    Processing…
  • very useful , thank u ..
    Are you sure you want to
    Your message goes here
    Processing…
  • super .. its very useful for me..
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Export data gridview to excel in c# Export data gridview to excel in c# Document Transcript

  • 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++){
  • 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
  • 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
  • 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