Following is the source code for
these two methods:
private void ExportToExcel()
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;
filename=Employee.xls");
Response.ContentType = "application/ms-excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
HtmlForm htmlForm = new HtmlForm();
GridView1.Parent.Controls.Add(htmlForm);
htmlForm.Attributes["runat"] = "server";
htmlForm.Controls.Add(GridView1);
htmlForm.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
private void ExportToWord()
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;
filename=Employee.doc");
Response.ContentType = "application/ms-word";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
HtmlForm htmlForm = new HtmlForm();
Displaying Views of Data (Part I)
[ 172 ]
GridView1.Parent.Controls.Add(htmlForm);
htmlForm.Attributes["runat"] = "server";
htmlForm.Controls.Add(GridView1);
htmlForm.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
Refer to the code snippets above. The Response.ClearContent() method is
used to erase the content in the Response object.
Pages:
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173