将数据导出成excel表

/// <summary>
/// 生成excel表
/// </summary>
/// <param name="dt">数据表</param>
private void PutOutDoc(DataTable dt)
{

GridView GV1 = new GridView();
GV1.DataSource = dt.DefaultView;
GV1.DataBind();

Response.Clear();

Response.Buffer = true;

Response.Charset = "UTF-8";
string fileName = DateTime.Now.ToString() + "导出签到记录";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls");

Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");

Response.ContentType = "application/vnd.ms-excel; charset=UTF-8";

EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();

System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);

GV1.RenderControl(hw);

Response.Write(tw.ToString());

Response.End();

}

posted @ 2013-11-02 00:55  BicycleBoy  阅读(214)  评论(0编辑  收藏  举报