Click here to Skip to main content
Licence 
First Posted 22 Feb 2005
Views 176,931
Bookmarked 51 times

Export DataGrid to Excel

By | 2 Mar 2005 | Article
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.

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

JJ G

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberLodJordan2:05 30 Sep '11  
GeneralMy vote of 5 Pinmemberdarshan yadav0:33 8 Mar '11  
GeneralMy vote of 2 Pinmemberdcustode21:35 28 Jul '10  
GeneralBetter use a non-COM solution Pinmemberpfgrid8:52 18 Jul '10  
GeneralVB.net Solution PinmemberTHROWBACK454:03 9 Jun '10  
Questionif my datagrid is from xml? Pinmembervirdigs23:21 24 May '10  
Generalexport to excel Pinmembersdeepa23:34 1 Dec '08  
QuestionMake column fonts BOLD and format text ?? Pinmembergurdeeptoor9:38 21 Apr '08  
GeneralDatagrid Pinmemberdddaaannn8:40 25 Mar '08  
AnswerRe: Datagrid Pinmembershamahamid7:26 9 Jul '08  
GeneralRe: Datagrid PinmemberWamuti22:44 3 Sep '09  
GeneralRe: Datagrid Pinmemberdarshan yadav1:59 8 Mar '11  
GeneralRe: Datagrid Pinmembergg42372:19 23 Oct '09  
Generaldatagrid data to excel PinmemberJose Lora11:39 9 Jan '08  
Questionsame code used with datagrid? Pinmemberspidermike8:30 9 Jan '08  
GeneralSolution International users Pinmemberxor.be2:16 14 Nov '07  
GeneralRe: Solution International users PinmemberWalaza21:34 16 Mar '08  
Questionconvert xml data to excel format Pinmembershajoh23:51 11 Apr '07  
QuestionWhat if I use an ArrayList as the DataSource? PinmemberSeFe3:51 11 Apr '07  
QuestionDataGrid to excel in c#.net Pinmemberashwinishilpa0:15 9 Oct '06  
QuestionHow To Customize Cells Styles PinmemberGianniCic21:51 31 Aug '06  
GeneralIf we want to create two worksheet Pinmemberserkanuz5:45 14 Aug '06  
GeneralRe: If we want to create two worksheet PinmemberFilipKrnjic4:46 9 Jul '09  
GeneralException and warning PinmemberAsbj0rn7:39 20 Jul '06  
GeneralRe: Exception and warning PinmemberAsbj0rn9:10 20 Jul '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Mar 2005
Article Copyright 2005 by JJ G
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid