Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / C#
Tip/Trick

Exporting DataGridview To Excel

Rate me:
Please Sign up or sign in to vote.
4.79/5 (26 votes)
17 Feb 2013CPOL 214K   2   32   24
How to export DataGridView data to Excel.

Introduction

This article is for beginners. In this article I am going to explain how to export DataGridView data to Excel.

Background

Initially I was stuck in this problem for a few days then after searching for a few days on Google and on some other sites, I got my answer and here it is. Hope you will like it.

Using the code

References:

C#
using 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 MySql.Data.MySqlClient;
using System.Configuration;
using System.IO;

Function for exporting to Excel:

C#
private void ToCsV(DataGridView dGV, string filename)
        {
            string stOutput = "";
            // Export titles:
            string sHeaders = "";

            for (int j = 0; j < dGV.Columns.Count; j++)
                sHeaders = sHeaders.ToString() + Convert.ToString(dGV.Columns[j].HeaderText) + "\t";
            stOutput += sHeaders + "\r\n";
            // Export data.
            for (int i = 0; i < dGV.RowCount - 1; i++)
            {
                string stLine = "";
                for (int j = 0; j < dGV.Rows[i].Cells.Count; j++)
                    stLine = stLine.ToString() + Convert.ToString(dGV.Rows[i].Cells[j].Value) + "\t";
                stOutput += stLine + "\r\n";
            }
            Encoding utf16 = Encoding.GetEncoding(1254);
            byte[] output = utf16.GetBytes(stOutput);
            FileStream fs = new FileStream(filename, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(output, 0, output.Length); //write the encoded file
            bw.Flush();
            bw.Close();
            fs.Close();
        }  

Code for button for exporting to Excel

C#
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Excel Documents (*.xls)|*.xls";
sfd.FileName = "export.xls";
if (sfd.ShowDialog() == DialogResult.OK)
{
    //ToCsV(dataGridView1, @"c:\export.xls");
    ToCsV(dataGridView1, sfd.FileName); // Here dataGridview1 is your grid view name
}

In my next article I am going to show you how to export multiple DataGridView on the same form using C#. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionUTF8 not support Pin
Member 105985976-Sep-19 6:35
Member 105985976-Sep-19 6:35 
PraiseAppriciation Pin
Member 1409231627-Jan-19 5:11
Member 1409231627-Jan-19 5:11 
QuestionWorks on Net 3.5, great work... Pin
Banee Ishaque K5-Apr-18 15:29
professionalBanee Ishaque K5-Apr-18 15:29 
QuestionThank You! Pin
jammy_VB.net25-Aug-17 13:39
jammy_VB.net25-Aug-17 13:39 
BugLittle Bug Pin
ulungss26-Sep-16 23:27
ulungss26-Sep-16 23:27 
SuggestionNew Nuget Package Pin
Vladimir Venegas19-Feb-16 6:15
Vladimir Venegas19-Feb-16 6:15 
GeneralParabéns! Pin
thifsantonio28-Jul-15 2:55
thifsantonio28-Jul-15 2:55 
SuggestionUse a StringBuilder instead of a string Pin
JLuis Estrada30-Jun-15 10:42
JLuis Estrada30-Jun-15 10:42 
Questionthanks and question Pin
Alaa Fadhel4-May-15 10:25
Alaa Fadhel4-May-15 10:25 
AnswerRe: thanks and question Pin
Adel Khalil1-Nov-16 22:23
Adel Khalil1-Nov-16 22:23 
GeneralReally Helpful... Pin
Member 397940420-Feb-15 10:38
Member 397940420-Feb-15 10:38 
QuestionComment Pin
Hemanth5778-Feb-15 18:28
Hemanth5778-Feb-15 18:28 
Generalmy vote 5 Pin
ihsansaidi15-Jan-15 20:16
ihsansaidi15-Jan-15 20:16 
QuestionRefined Pin
MachoMan6667-Jan-15 18:21
MachoMan6667-Jan-15 18:21 
Questiononly table column names were displaying Pin
chaitanya5566-Jul-14 20:28
chaitanya5566-Jul-14 20:28 
GeneralMy vote of 1 Pin
Member 1042568223-Apr-14 20:57
Member 1042568223-Apr-14 20:57 
QuestionAdvice Pin
Member 1072402017-Apr-14 21:24
Member 1072402017-Apr-14 21:24 
AnswerRe: Advice Pin
Adel Khalil1-Nov-16 22:26
Adel Khalil1-Nov-16 22:26 
QuestionOne suggestion Pin
erry1ne4-Oct-13 1:53
erry1ne4-Oct-13 1:53 
GeneralRe: One suggestion Pin
PIEBALDconsult4-Oct-13 2:12
mvePIEBALDconsult4-Oct-13 2:12 
SuggestionSome code suggestions... Pin
kjhf11-Aug-13 23:53
kjhf11-Aug-13 23:53 
GeneralRe: Some code suggestions... Pin
Member 1107934310-Jan-15 3:53
Member 1107934310-Jan-15 3:53 
SuggestionLast Line in gridview is missed Pin
Aymen RMILI22-Jun-13 5:33
Aymen RMILI22-Jun-13 5:33 
QuestionThere is a good method to export DataGrid or Data GridView to excel. PinPopular
geeta goel18-Feb-13 0:58
geeta goel18-Feb-13 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.