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

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();

DataTable table = DATASETNAME.Tables[0];
int ColumnIndex=0; 
foreach(Datacolumn col in table.Columns)
int rowIndex=0; 
foreach(DataRow row in table.Row) 
    foreach(DataColumn col in table.Columns)         
excel.Visible = true; 
Worksheet worksheet = (Worksheet)excel.ActiveSheet; 

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


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


About the Author

Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberLodJordan30-Sep-11 3:05 
GeneralMy vote of 5 Pinmemberdarshan yadav8-Mar-11 1:33 
GeneralMy vote of 2 Pinmemberdcustode28-Jul-10 22:35 
GeneralBetter use a non-COM solution Pinmemberpfgrid18-Jul-10 9:52 Solution PinmemberTHROWBACK459-Jun-10 5:03 
Questionif my datagrid is from xml? Pinmembervirdigs25-May-10 0:21 
Generalexport to excel Pinmembersdeepa2-Dec-08 0:34 
QuestionMake column fonts BOLD and format text ?? Pinmembergurdeeptoor21-Apr-08 10:38 
GeneralDatagrid Pinmemberdddaaannn25-Mar-08 9:40 
AnswerRe: Datagrid Pinmembershamahamid9-Jul-08 8:26 
GeneralRe: Datagrid PinmemberWamuti3-Sep-09 23:44 
GeneralRe: Datagrid Pinmemberdarshan yadav8-Mar-11 2:59 
GeneralRe: Datagrid Pinmembergg423723-Oct-09 3:19 
Generaldatagrid data to excel PinmemberJose Lora9-Jan-08 12:39 
Questionsame code used with datagrid? Pinmemberspidermike9-Jan-08 9:30 
GeneralSolution International users Pinmemberxor.be14-Nov-07 3:16 
GeneralRe: Solution International users PinmemberWalaza16-Mar-08 22:34 
Questionconvert xml data to excel format Pinmembershajoh12-Apr-07 0:51 
QuestionWhat if I use an ArrayList as the DataSource? PinmemberSeFe11-Apr-07 4:51 
QuestionDataGrid to excel in Pinmemberashwinishilpa9-Oct-06 1:15 
QuestionHow To Customize Cells Styles PinmemberGianniCic31-Aug-06 22:51 
GeneralIf we want to create two worksheet Pinmemberserkanuz14-Aug-06 6:45 
GeneralRe: If we want to create two worksheet PinmemberFilipKrnjic9-Jul-09 5:46 
GeneralException and warning PinmemberAsbj0rn20-Jul-06 8:39 
GeneralRe: Exception and warning PinmemberAsbj0rn20-Jul-06 10:10 

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.

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