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



My Profile

Gifts

Active Members
TodayLast 7 Days more...









Export Data From Grid.


Posted Date: 27 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Bharathi       Member Level: Diamond
Rating:     Points: 5



Export the data from Grid:

Grid control in ASP.Net is one of the controls used for populating large amount of data. It would be better if there is a simple way to export the data to Excel, Word or pdf format.

This article will show you, how to export the records in grid to excel, word etc.

Exporting Data to EXCEL:

Export data to excel can be done easily with the Grid control using RenderControl () function of GridView. Example of exporting data to excel is shown below:




Response.Clear();
Response.AddHeader("content-disposition", attachment;filename=TEST.xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
Grid1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();


Response.AddHeader - It helps to add the content header, where "content-disposition", helps to specify how the response will be handled.
Specifying the filename will let the Asp.Net to know that it is exporting a file with a name “TEST.xls”.

Response.ContentType – It identifies the export type. Here application/.xls specifies the format of the file as excel.

Grid1.Rendercontrol () – This function converts the content of grid to html format and initialize in the htmlwriter htmlwrite.

Response.Write () – will write the content to a webpage and provide a way to save the rendered data with a help of Dialogue box.


Now the grid made easy to export the data to excel.


Exporting Data to Word:

Exporting grid data to Word is as simple as EXCEL, the same code will help to export the data to word instead replace the vnd.excel with vnd.doc or vnd.word. The code is as below:


Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
myDataGrid.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End ();

Exporting Data to PDF:

The same way replace the vnd.word with vnd.pdf, to export to PDF file.
Export the Data to File and save to hard disk
The above code will provide a dialogue box to open/save the data to in specified format. Sometimes there may be a requirement to save the file implicitly without asking the user to save/open. The following code will provide a solution for this requirement:

string filenameWithPath ;

StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
myDataGrid.RenderControl(hw);
HtmlInfo = sw.ToString().Trim();

filenameWithPath = "h://TestExcel.xls" ;

File.Delete(filename);
FileStream fs = new FileStream(filenameWithPath, FileMode.Create);
StreamWriter SWriter = new StreamWriter(fs, System.Text.Encoding.GetEncoding("UTF-8"));
SWriter.Write(sw.ToString());
SWriter.Close();
fs.Close();







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

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: Working with Isolated Storage
Previous Resource: CAPTCHA in VB.NET
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design   Scripts

doors in nj

Contact Us    Privacy Policy    Terms Of Use