ViewState["OutPut"] = "";//定义一个变量用于存储查询条件
/// <summary>
/// 新建一个新的Gridview用于导出
/// </summary>
/// <param name="where">导出条件</param>
private GridView OutPutBinder(string where)
{
GridView uxGridViewOutPut = new GridView();
uxGridViewOutPut.DataSource = CardBLL.GetALL(where, "");
uxGridViewOutPut.DataBind();
return uxGridViewOutPut;
}
/// <summary>
/// 导出到EXCEL
/// </summary>
/// <param name="FileName">导出的文件名</param>
public void OutPutExcel(string FileName)
{
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
OutPutBinder(ViewState["OutPut"].ToString()).RenderControl(oHtmlTextWriter);
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
Response.Write(oStringWriter.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}