C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Exporting Data From GridView/DataSet to Word/Excel


Posted Date: 27 May 2008    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: srilu       Member Level: Diamond
Rating:     Points: 10



Using this code you can export data from Gridview or dataset to excel or word.

//get data from database
SqlDataAdapter da = new SqlDataAdapter("select * from emp", con);
DataSet ds = new DataSet();
da.Fill(ds);
//fill it in a gridview
GridView gd = new GridView();
gd.ID = "GridView1";
gd.DataSource = source;
gd.AutoGenerateColumns = true;
gd.DataBind();
//export that data to word/excel
Response.Clear();
Response.Buffer = true;
switch (type)
{
case ExportType.EXCEL:
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "ExportedGridData"));
break;

case ExportType.WORD:
Response.ContentType = "application/ms-word";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.doc", "ExportedGridData"));
break;
}
Response.Charset = "";
System.IO.StringWriter stringwriter = new StringWriter();

HtmlTextWriter htmlwriter = new HtmlTextWriter(stringwriter);
gd.RenderControl(htmlwriter);
Response.Write(stringwriter.ToString());
Response.End();




Responses

Author: vijetha    29 May 2008Member Level: Gold   Points : 2
hi,
This code is nice


Author: Bachan rawat    30 May 2008Member Level: Silver   Points : 2
its nice;
Thanks


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: how to bind Data to GridView
Previous Resource: How to display sum of particular column in GridView Footer.
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use