Export Excel Button Events
protected void Button1_Click (object sender, EventArgs e)
{
Export ("Application/ms-excel", "farmland environmental data. xls");
}
Export Word button Events
protected void button2_click (object sender, EventArgs e)
{
Export ("Application/ms-excel", "Farmland environmental data. doc");
Export ("Application/ms-word", "Farmland environmental data. doc");//both can
}
private void Export (String FileType, String FileName)
{
Response.Charset = "GB2312";
response.contentencoding = System.Text.Encoding.UTF8;
Response.appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode (filename, Encoding.UTF8). ToString ());
Response.ContentType = FileType;
This. EnableViewState = false;
StringWriter tw = new StringWriter ();
HtmlTextWriter HW = new HtmlTextWriter (TW);
Gridview1.rendercontrol (HW); Response.Write (TW. ToString ());
Response.End ();
}
This method must be overridden, or an error will occur
public override void Verifyrenderinginserverform (Control control)
{
}
C # GridView Export Excel and Word files