protected void Button1_Click(object sender, EventArgs e)
{
this.GridView1.AllowPaging = false;
this.GridView1.AllowSorting = false;
toExcel(this.GridView1);
// toExcel(this.GridView1, begin1.ToShortDateString() + "至" + end1.ToShortDateString() + "转出表");
this.GridView1.AllowPaging = true;
this.GridView1.AllowSorting = true;
}
/// <summary>
/// 导出到Excel
/// </summary>
/// <param name="gv"></param>
void toExcel(GridView GridView1)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
string fileName = "export.xls";
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
this.GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
/// <summary>
/// 这个重写貌似是必须的
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control) {
}