Click here to Skip to main content
15,881,139 members
Articles / Programming Languages / C#
Article

Export DataGrid to Excel

Rate me:
Please Sign up or sign in to vote.
3.08/5 (40 votes)
2 Mar 2005CPOL 260.8K   53   53
Easily export DataGrid data to Excel.

Export to Excel (Windows forms only)

This is basically the easiest way to export data from a DataGrid or DataSet to Excel.

I looked all over the Internet and could not find anything useful, only ASP.NET ways of exporting. You just need to add to Excel DLL your references. I have looked over the Internet for the easiest way of doing it and at the end ended up doing this. Just put the code where ever you want to call the event that export the Dataset or DataGrid to Excel.

C#
Excel.ApplicationClass excel = new ApplicationClass();

excel.Application.Workbooks.Add(true); 
DataTable table = DATASETNAME.Tables[0];
int ColumnIndex=0; 
foreach(Datacolumn col in table.Columns)
{   
   ColumnIndex++;
   excel.Cells[1,ColumnIndex]=col.ColumnName;
} 
int rowIndex=0; 
foreach(DataRow row in table.Row) 
{         
    rowIndex++;       
    ColumnIndex=0;         
    foreach(DataColumn col in table.Columns)         
    {  
        ColumnIndex++;                
        excel.Cells[rowIndex+1,ColumnIndex]=row.Cells[col.ColumnName].Text;         
    }
} 
excel.Visible = true; 
Worksheet worksheet = (Worksheet)excel.ActiveSheet; 
worksheet.Activate();
)

I know its not the most difficult thing on the planet to make, but it can be useful to beginners.

License

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Exception and warning Pin
Ilove.net24-Jan-07 11:03
Ilove.net24-Jan-07 11:03 
GeneralRe: Exception and warning Pin
chrisbal24-Jan-07 11:25
chrisbal24-Jan-07 11:25 
GeneralRe: Exception and warning Pin
Ilove.net24-Jan-07 11:46
Ilove.net24-Jan-07 11:46 
GeneralRe: Exception and warning Pin
Ilove.net24-Jan-07 10:53
Ilove.net24-Jan-07 10:53 
GeneralHelp! Pin
congema12-Jul-06 12:20
congema12-Jul-06 12:20 
GeneralRe: Help! Pin
Dr R15-Jul-06 11:38
Dr R15-Jul-06 11:38 
GeneralRe: Help! Pin
congema18-Jul-06 7:25
congema18-Jul-06 7:25 
GeneralThanks Pin
RaviChekuru14-Mar-06 21:28
RaviChekuru14-Mar-06 21:28 
Thanks a lot. This was very useful to me

Ravi
GeneralCode Fixes Pin
Lucradev11-Jan-06 17:37
Lucradev11-Jan-06 17:37 
Generalthe correct assignment in the foreach loop Pin
Anonymous7-Oct-05 4:47
Anonymous7-Oct-05 4:47 
Generalan optional "using" Pin
Anonymous7-Oct-05 3:38
Anonymous7-Oct-05 3:38 
GeneralRe: an optional "using" Pin
Asbj0rn22-Jul-06 1:03
Asbj0rn22-Jul-06 1:03 
GeneralAlternative way Pin
Jan Gex23-Sep-05 6:14
Jan Gex23-Sep-05 6:14 
QuestionThanks but can it be more? Pin
Member 200611919-Jul-05 0:33
Member 200611919-Jul-05 0:33 
AnswerRe: Thanks but can it be more? Pin
gcavin27-Apr-06 4:46
gcavin27-Apr-06 4:46 
Generalsome errors Pin
melissah16-Mar-05 16:38
melissah16-Mar-05 16:38 
GeneralRe: some errors Pin
JJ G18-Mar-05 0:08
JJ G18-Mar-05 0:08 
GeneralRe: some errors Pin
Stephen McAllister1-Apr-05 9:55
Stephen McAllister1-Apr-05 9:55 
GeneralASP.NET Pin
NStratton8-Mar-05 20:47
sussNStratton8-Mar-05 20:47 
GeneralRe: ASP.NET Pin
JJ G8-Mar-05 21:20
JJ G8-Mar-05 21:20 
GeneralRe: ASP.NET Pin
Nigel Stratton9-Mar-05 3:51
Nigel Stratton9-Mar-05 3:51 
GeneralRe: ASP.NET Pin
Nigel Stratton9-Mar-05 5:38
Nigel Stratton9-Mar-05 5:38 
GeneralRe: ASP.NET Pin
JJ G9-Mar-05 19:52
JJ G9-Mar-05 19:52 
GeneralRe: ASP.NET Pin
Ilove.net24-Jan-07 10:56
Ilove.net24-Jan-07 10:56 
GeneralRe: ASP.NET Pin
Ilove.net24-Jan-07 11:43
Ilove.net24-Jan-07 11:43 

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.