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();
|
| Author: Redhat 15 Apr 2009 | Member 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
|