ASP.ENT c#データをExcelにエクスポート


public void ToExcel(DataTable dt,HttpContext content)
        {
            string filename = DateTime.Now.ToString("yyyyMMddHHmmss");
            content.Response.Clear();
            string fileName = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename));
            content.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".xls");
            content.Response.ContentType = "applicationnd.ms-excel";
            content.Response.Charset = "utf-8";
            StringBuilder s = new StringBuilder();
            s.Append("" + fileName + "");

            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
            System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();

            dg.DataSource = dt;
            dg.DataBind();
            dg.RenderControl(htw);
            content.Response.Write(sw.ToString());
            content.Response.End();
        }