C# DataGridView Export to Excel (Dem Exe)

3
04/09/13 C# DataGridView Export to Excel csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm 1/3 Net-informations.com Home VB.NET C# ASP.NET AJAX .Net Framework Interview Questions About Share Download Source Code Print Source Code Displaying data in a tabular format is a task you are likely to perform frequently. The DataGridView control is designed to be a complete solution for displaying tabular data with Windows Forms. The DataGridView control is highly configurable and extensible, and it provides many properties, methods, and events to customize its appearance and behavior. The following C# source code shows how to Export the content of a datagridview to an Excel file. Next : C# DataGridView Loading data from Excel using System; using System.Data; using System.Windows.Forms; using System.Data.SqlClient; using Excel = Microsoft.Office.Interop.Excel; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;In string sql = "SELECT * FROM Authors"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection) DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Authors_table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Authors_table"; } private void button2_Click(object sender, EventArgs e) { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; Int16 i, j; xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1) for (i = 0; i <= dataGridView1.RowCount - 2; i++) { for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) { xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Va Advertisement An overview of Microsoft C# C# Language Tutorial C# Statements Tutorial C# Graphical User Interface Ads by safesaver C# Collection Tutorial About | SiteMap C# DataGridView Export to Excel

Transcript of C# DataGridView Export to Excel (Dem Exe)

Page 1: C# DataGridView Export to Excel (Dem Exe)

04/09/13 C# DataGridView Export to Excel

csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm 1/3

Net-informations.com

Home VB.NET C# ASP.NET AJAX .Net Framework Interview Questions About

Share

Download Source Code Print Source Code

Displaying data in a tabular format is a task you are likely to performfrequently. The DataGridView control is designed to be a completesolution for displaying tabular data with Windows Forms. TheDataGridView control is highly configurable and extensible, and it providesmany properties, methods, and events to customize its appearance andbehavior. The following C# source code shows how to Export the contentof a datagridview to an Excel file.

Next : C# DataGridView Loading data from Excel

using System;using System.Data;using System.Windows.Forms;using System.Data.SqlClient;using Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { string connectionString = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"; string sql = "SELECT * FROM Authors"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection); DataSet ds = new DataSet(); connection.Open(); dataadapter.Fill(ds, "Authors_table"); connection.Close(); dataGridView1.DataSource = ds; dataGridView1.DataMember = "Authors_table"; }

private void button2_Click(object sender, EventArgs e) { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value;

Int16 i, j;

xlApp = new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue);

xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

for (i = 0; i <= dataGridView1.RowCount - 2; i++) { for (j = 0; j <= dataGridView1.ColumnCount - 1; j++) { xlWorkSheet.Cells[i + 1, j + 1] = dataGridView1[j, i].Value.ToString();

Advertisement

An overview of Microsoft C#

C# Language Tutorial

C# Statements Tutorial

C# Graphical User Interface

Ads by safesaver

C# Collection Tutorial

About | SiteMap

C# DataGridView Export to Excel

Page 2: C# DataGridView Export to Excel (Dem Exe)

04/09/13 C# DataGridView Export to Excel

csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm 2/3

} }

xlWorkBook.SaveAs(@"c:\csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit();

releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); }

private void releaseObject(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(); } } }}

C# DataGridView - Related Contents

C# DataGridView Binding - SQL Server dataset

C# DataGridView Binding - OLEDB dataset

C# DataGridView Sorting and Filtering

C# DataGridView Add Columns and Rows

C# DataGridView Hide Columns and Rows

C# DataGridView Read Only Columns and Rows

Add Button to C# DataGridView

Add CheckBox to C# DataGridView

Add ComboBox to C# DataGridView

Add Image to C# DataGridView

Add ViewLink to C# DataGridView

C# DataGridView Paging

C# DataGridView Formatting

C# DataGridView Template

C# DataGridView Printing

C# DataGridView Loading data from Excel

C# DataGridView Database Operations

C# String Tutorial

C# File Operations Tutorial

C# Excel Tutorial

C# Crystal Reports Tutorial

CSharp Communication Tutorial

C# Ado.Net Tutorial and Source Code

C# ADO.NET data Providers Tutorial

C# Dataset Tutorial

C# DataAdapater Tutorial

Csharp DataView Tutorial

Csharp Remoting Tutorial

C# XML Tutorial

C# DataGridView Tutorial

Page 3: C# DataGridView Export to Excel (Dem Exe)

04/09/13 C# DataGridView Export to Excel

csharp.net-informations.com/datagridview/csharp-datagridview-export-excel.htm 3/3

More Source Code : Search Mail to : [email protected]

Ads by safesaver

net-informations.com (C) 2013 Founded by raps mk All Rights Reserved. All other trademarks are property of their respective owners.