How to export datagridview to excel format using c#


How to export datagridview to excel format using c#?In this article I'm going to explain how to convert datagridview to excel format using c#. This is a requirement often in many projects as users may want to save some data generated in applications for future reference.

How to export datagridview to excel format using c#?In this article I'm going to explain how to convert datagridview to excel format using c#. This is a requirement often in many projects as users may
want to save some data generated in applications for future reference.
Just drag and drop one datagridview and button control to your window application.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
System.Data.DataTable dtblSkill = new System.Data.DataTable();
public Form1()
{
InitializeComponent();
dtblSkill.Columns.Add("SkillCode", typeof(string));
dtblSkill.Columns.Add("SkillName", typeof(string));
AddGridViewSkillColumns();
}
private void AddGridViewSkillColumns()
{
try
{

DataGridViewTextBoxColumn dgvtxtSkillCode = new DataGridViewTextBoxColumn();
dgvtxtSkillCode.Name = "SkillCode";
dgvtxtSkillCode.DataPropertyName = "SkillCode";
dgvtxtSkillCode.HeaderText = "Skill Code";
dgvtxtSkillCode.Width = 120;


DataGridViewTextBoxColumn dgvtxtSkillName = new DataGridViewTextBoxColumn();
dgvtxtSkillName.Name = "SkillName";
dgvtxtSkillName.HeaderText = "Skill Name";
dgvtxtSkillName.DataPropertyName = "SkillName";
dgvtxtSkillName.Visible = true;
dgvtxtSkillName.Width = 160;


dataGridView1.Columns.Add(dgvtxtSkillCode);
dataGridView1.Columns.Add(dgvtxtSkillName);

dataGridView1.DataSource = dtblSkill;
}
catch (Exception ex)
{
MessageBox.Show("Error Details" + ex.Message);
}
}

private void button1_Click(object sender, EventArgs e)
{
Microsoft.Office.Interop.Excel.Application wapp;
Microsoft.Office.Interop.Excel.Worksheet wsheet;
Microsoft.Office.Interop.Excel.Workbook wbook;
wapp = new Microsoft.Office.Interop.Excel.Application();
wapp.Visible = false;
wbook = wapp.Workbooks.Add(true);
wsheet = (Worksheet)wbook.ActiveSheet;
try
{

for (int i = 0; i < this.dataGridView1.Columns.Count; i++)
{
wsheet.Cells[1, i + 1] = this.dataGridView1.Columns[i].HeaderText;

}

for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
DataGridViewRow dgvrow = this.dataGridView1.Rows[i];
for (int j = 0; j < dgvrow.Cells.Count; j++)
{
DataGridViewCell cellvalue = dgvrow.Cells[j];
try
{
wsheet.Cells[i + 2, j + 1] = (cellvalue.Value == null) ? "" : cellvalue.Value.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
wapp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show("Error Details" + ex.Message);
}
}
}
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: