Subscribe to Subscribers

Online Members

Shine S
More...

Resources » .NET programming » .NET Framework

Export Data From Grid.


Posted Date:     Category: .NET Framework    
Author: Member Level: Gold    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();





Did you like this resource? Share it with your friends and show your love!


Responses to "Export Data From Grid."
Author: Redhat    15 Apr 2009Member Level: Silver   Points : 1
The same code as mentioned by u

Response.ContentType = "application/vnd.pdf"

is not working for pdf , i am getting that the pdf file is
invaid like that error , but it works fine for word and excel
can u plz give me ur suggestions

Thanks



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Working with Isolated Storage
    Previous Resource: CAPTCHA in VB.NET
    Return to Resources
    Post New Resource
    Category: .NET Framework


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.