
Code
private void GridViewToExcel()
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=WebOrderExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 解决前导0丢失问题
string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.Page.EnableViewState = false;
tw.WriteLine(strStyle);
GridView1.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
/// <summary>
/// 解决 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewToExcel();
}
posted @
2008-11-18 20:31
.net's
阅读(
411)
评论()
收藏
举报