Click here to Skip to main content
13,201,940 members (74,931 online)
Click here to Skip to main content
Add your own
alternative version

Stats

230.1K views
52 bookmarked
Posted 22 Feb 2005

Export DataGrid to Excel

, 2 Mar 2005
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

 
GeneralMy vote of 5 Pin
LodJordan30-Sep-11 2:05
memberLodJordan30-Sep-11 2:05 
GeneralMy vote of 5 Pin
darshan yadav8-Mar-11 0:33
memberdarshan yadav8-Mar-11 0:33 
GeneralMy vote of 2 Pin
dcustode28-Jul-10 21:35
memberdcustode28-Jul-10 21:35 
GeneralBetter use a non-COM solution Pin
pfgrid18-Jul-10 8:52
memberpfgrid18-Jul-10 8:52 
GeneralVB.net Solution Pin
THROWBACK459-Jun-10 4:03
memberTHROWBACK459-Jun-10 4:03 
Questionif my datagrid is from xml? Pin
virdigs24-May-10 23:21
membervirdigs24-May-10 23:21 
Generalexport to excel Pin
sdeepa1-Dec-08 23:34
membersdeepa1-Dec-08 23:34 
QuestionMake column fonts BOLD and format text ?? Pin
gurdeeptoor21-Apr-08 9:38
membergurdeeptoor21-Apr-08 9:38 
GeneralDatagrid Pin
dddaaannn25-Mar-08 8:40
memberdddaaannn25-Mar-08 8:40 
AnswerRe: Datagrid Pin
shamahamid9-Jul-08 7:26
membershamahamid9-Jul-08 7:26 

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.

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