Click here to Skip to main content
12,405,946 members (45,818 online)
Click here to Skip to main content
Add your own
alternative version

Stats

218.1K views
52 bookmarked
Posted

Export DataGrid to Excel

, 2 Mar 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

JJ G
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralRe: Datagrid Pin
Wamuti3-Sep-09 22:44
memberWamuti3-Sep-09 22:44 
GeneralRe: Datagrid Pin
darshan yadav8-Mar-11 1:59
memberdarshan yadav8-Mar-11 1:59 
GeneralRe: Datagrid Pin
gg423723-Oct-09 2:19
membergg423723-Oct-09 2:19 
Generaldatagrid data to excel Pin
Jose Lora9-Jan-08 11:39
memberJose Lora9-Jan-08 11:39 
Questionsame code used with datagrid? Pin
spidermike9-Jan-08 8:30
memberspidermike9-Jan-08 8:30 
GeneralSolution International users Pin
xor.be14-Nov-07 2:16
memberxor.be14-Nov-07 2:16 
GeneralRe: Solution International users Pin
Walaza16-Mar-08 21:34
memberWalaza16-Mar-08 21:34 
Questionconvert xml data to excel format Pin
shajoh11-Apr-07 23:51
membershajoh11-Apr-07 23:51 
QuestionWhat if I use an ArrayList as the DataSource? Pin
SeFe11-Apr-07 3:51
memberSeFe11-Apr-07 3:51 
QuestionDataGrid to excel in c#.net Pin
ashwinishilpa9-Oct-06 0:15
memberashwinishilpa9-Oct-06 0:15 
QuestionHow To Customize Cells Styles Pin
GianniCic31-Aug-06 21:51
memberGianniCic31-Aug-06 21:51 
GeneralIf we want to create two worksheet Pin
serkanuz14-Aug-06 5:45
memberserkanuz14-Aug-06 5:45 
GeneralRe: If we want to create two worksheet Pin
FilipKrnjic9-Jul-09 4:46
memberFilipKrnjic9-Jul-09 4:46 
GeneralException and warning Pin
Asbj0rn20-Jul-06 7:39
memberAsbj0rn20-Jul-06 7:39 
GeneralRe: Exception and warning Pin
Asbj0rn20-Jul-06 9:10
memberAsbj0rn20-Jul-06 9:10 
AnswerRe: Exception and warning Pin
chrisbal24-Jan-07 10:40
memberchrisbal24-Jan-07 10:40 
GeneralRe: Exception and warning Pin
Ilove.net24-Jan-07 11:03
memberIlove.net24-Jan-07 11:03 
GeneralRe: Exception and warning Pin
chrisbal24-Jan-07 11:25
memberchrisbal24-Jan-07 11:25 
GeneralRe: Exception and warning Pin
Ilove.net24-Jan-07 11:46
memberIlove.net24-Jan-07 11:46 
GeneralRe: Exception and warning Pin
Ilove.net24-Jan-07 10:53
memberIlove.net24-Jan-07 10:53 
GeneralHelp! Pin
congema12-Jul-06 12:20
membercongema12-Jul-06 12:20 
GeneralRe: Help! Pin
Dr R15-Jul-06 11:38
memberDr R15-Jul-06 11:38 
GeneralRe: Help! Pin
congema18-Jul-06 7:25
membercongema18-Jul-06 7:25 
GeneralThanks Pin
RaviChekuru14-Mar-06 21:28
memberRaviChekuru14-Mar-06 21:28 
GeneralCode Fixes Pin
fdafdafdsa11-Jan-06 17:37
memberfdafdafdsa11-Jan-06 17:37 
Generalthe correct assignment in the foreach loop Pin
Anonymous7-Oct-05 4:47
sussAnonymous7-Oct-05 4:47 
Generalan optional "using" Pin
Anonymous7-Oct-05 3:38
sussAnonymous7-Oct-05 3:38 
GeneralRe: an optional "using" Pin
Asbj0rn22-Jul-06 1:03
memberAsbj0rn22-Jul-06 1:03 
GeneralAlternative way Pin
Jan Gex23-Sep-05 6:14
memberJan Gex23-Sep-05 6:14 
QuestionThanks but can it be more? Pin
Sudhir11119-Jul-05 0:33
memberSudhir11119-Jul-05 0:33 
AnswerRe: Thanks but can it be more? Pin
gcavin27-Apr-06 4:46
membergcavin27-Apr-06 4:46 
Generalsome errors Pin
melissah16-Mar-05 16:38
sussmelissah16-Mar-05 16:38 
GeneralRe: some errors Pin
JJRSA2118-Mar-05 0:08
memberJJRSA2118-Mar-05 0:08 
GeneralRe: some errors Pin
Stephen McAllister1-Apr-05 9:55
memberStephen McAllister1-Apr-05 9:55 
GeneralASP.NET Pin
NStratton8-Mar-05 20:47
sussNStratton8-Mar-05 20:47 
GeneralRe: ASP.NET Pin
JJRSA218-Mar-05 21:20
memberJJRSA218-Mar-05 21:20 
GeneralRe: ASP.NET Pin
StrattonN9-Mar-05 3:51
memberStrattonN9-Mar-05 3:51 
GeneralRe: ASP.NET Pin
StrattonN9-Mar-05 5:38
memberStrattonN9-Mar-05 5:38 
GeneralRe: ASP.NET Pin
JJRSA219-Mar-05 19:52
memberJJRSA219-Mar-05 19:52 
GeneralRe: ASP.NET Pin
Ilove.net24-Jan-07 10:56
memberIlove.net24-Jan-07 10:56 
GeneralRe: ASP.NET Pin
Ilove.net24-Jan-07 11:43
memberIlove.net24-Jan-07 11:43 
GeneralGreat thanks!! Pin
Nice Life23-Feb-05 1:17
memberNice Life23-Feb-05 1:17 
GeneralRe: Great thanks!! Pin
JJRSA217-Mar-05 1:51
memberJJRSA217-Mar-05 1:51 

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.

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