输出datagrid的内容到excel

private void OutPutToExcel()
        {
            Response.Clear();
            Response.Buffer = true;

            Response.Charset = "GB2312";
            Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
            this.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("FileName", System.Text.Encoding.UTF8) + ".xls");
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            StringWriter sw = new StringWriter(myCItrad);
            HtmlTextWriter writer = new HtmlTextWriter(sw);
            GridExcel.RenderControl(writer);
            Response.Write(sw.ToString());
            Response.End();
        }

原文地址:https://www.cnblogs.com/windstore/p/1584071.html