您的位置:首页 > 其它

GridViewToExcel

2008-11-18 20:31 525 查看
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();

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: