Click here to Skip to main content
Click here to Skip to main content

Export DataGrid to Excel

By , 2 Mar 2005
 

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

JJ G
Web Developer
United States United States
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberLodJordan30-Sep-11 2:05 
GeneralMy vote of 5memberdarshan yadav8-Mar-11 0:33 
GeneralMy vote of 2memberdcustode28-Jul-10 21:35 
GeneralBetter use a non-COM solutionmemberpfgrid18-Jul-10 8:52 
The the PFGrid.NET has this feature included without using a COM-libary. Excel must not be installed on your computer to use the feature:
 
PFGrid.NET
GeneralVB.net SolutionmemberTHROWBACK459-Jun-10 4:03 
Questionif my datagrid is from xml?membervirdigs24-May-10 23:21 
Generalexport to excelmembersdeepa1-Dec-08 23:34 
QuestionMake column fonts BOLD and format text ??membergurdeeptoor21-Apr-08 9:38 
GeneralDatagridmemberdddaaannn25-Mar-08 8:40 
AnswerRe: Datagridmembershamahamid9-Jul-08 7:26 
GeneralRe: DatagridmemberWamuti3-Sep-09 22:44 
GeneralRe: Datagridmemberdarshan yadav8-Mar-11 1:59 
GeneralRe: Datagridmembergg423723-Oct-09 2:19 
Generaldatagrid data to excelmemberJose Lora9-Jan-08 11:39 
Questionsame code used with datagrid?memberspidermike9-Jan-08 8:30 
GeneralSolution International usersmemberxor.be14-Nov-07 2:16 
GeneralRe: Solution International usersmemberWalaza16-Mar-08 21:34 
Questionconvert xml data to excel formatmembershajoh11-Apr-07 23:51 
QuestionWhat if I use an ArrayList as the DataSource?memberSeFe11-Apr-07 3:51 
QuestionDataGrid to excel in c#.netmemberashwinishilpa9-Oct-06 0:15 
QuestionHow To Customize Cells StylesmemberGianniCic31-Aug-06 21:51 
GeneralIf we want to create two worksheetmemberserkanuz14-Aug-06 5:45 
GeneralRe: If we want to create two worksheetmemberFilipKrnjic9-Jul-09 4:46 
GeneralException and warningmemberAsbj0rn20-Jul-06 7:39 
GeneralRe: Exception and warningmemberAsbj0rn20-Jul-06 9:10 
AnswerRe: Exception and warningmemberchrisbal24-Jan-07 10:40 
GeneralRe: Exception and warningmemberIlove.net24-Jan-07 11:03 
GeneralRe: Exception and warningmemberchrisbal24-Jan-07 11:25 
GeneralRe: Exception and warningmemberIlove.net24-Jan-07 11:46 
GeneralRe: Exception and warningmemberIlove.net24-Jan-07 10:53 
GeneralHelp!membercongema12-Jul-06 12:20 
GeneralRe: Help!memberDr R15-Jul-06 11:38 
GeneralRe: Help!membercongema18-Jul-06 7:25 
GeneralThanksmemberRaviChekuru14-Mar-06 21:28 
GeneralCode Fixesmemberfdafdafdsa11-Jan-06 17:37 
Generalthe correct assignment in the foreach loopsussAnonymous7-Oct-05 4:47 
Generalan optional "using"sussAnonymous7-Oct-05 3:38 
GeneralRe: an optional "using"memberAsbj0rn22-Jul-06 1:03 
GeneralAlternative waymemberJan Gex23-Sep-05 6:14 
QuestionThanks but can it be more?memberSudhir11119-Jul-05 0:33 
AnswerRe: Thanks but can it be more?membergcavin27-Apr-06 4:46 
Generalsome errorssussmelissah16-Mar-05 16:38 
GeneralRe: some errorsmemberJJRSA2118-Mar-05 0:08 
GeneralRe: some errorsmemberStephen McAllister1-Apr-05 9:55 
GeneralASP.NETsussNStratton8-Mar-05 20:47 
GeneralRe: ASP.NETmemberJJRSA218-Mar-05 21:20 
GeneralRe: ASP.NETmemberStrattonN9-Mar-05 3:51 
GeneralRe: ASP.NETmemberStrattonN9-Mar-05 5:38 
GeneralRe: ASP.NETmemberJJRSA219-Mar-05 19:52 
GeneralRe: ASP.NETmemberIlove.net24-Jan-07 10:56 

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

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