how to export datagridview to excel file - csharp-excel-datagridview

Download How to Export DataGridView to Excel File - Csharp-excel-datagridview

If you can't read please download the document

Upload: berk-cornie

Post on 25-Dec-2015

240 views

Category:

Documents


9 download

DESCRIPTION

Este ejemplo muestra como exportar un datagridvied a un archivo excel

TRANSCRIPT

How to export DataGridView to excel fileusing System;using System.Windows.Forms;using System.Data;using System.Data.SqlClient;using Excel = Microsoft.Office.Interop.Excel; namespace WindowsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection cnn ; string connectionString = null; string sql = null; connectionString = "data source=servername;initial catalog=databasename;user id=username;password=password;"; cnn = new SqlConnection(connectionString); cnn.Open(); sql = "SELECT * FROM Product"; SqlDataAdapter dscmd = new SqlDataAdapter(sql, cnn); DataSet ds = new DataSet(); dscmd.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; } private void button2_Click(object sender, EventArgs e) { Excel.Application xlApp ; Excel.Workbook xlWorkBook ; Excel.Worksheet xlWorkSheet ; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int i = 0; int j = 0; for (i = 0; i