引用:
using System.IO;
事件方法:
protected void toExcel(GridView GridView1)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
string fileName = "export.xlsx";
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)
{
}
导出命令:
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;
}