c#导出Excel

 protected void btnExport_Click(object sender, EventArgs e)

        {

            DataTable dt = (DataTable)ViewState["TableAll"];

            DataTable dtSerch = (DataTable)ViewState["SerchTable"];

            if (dtSerch != null)

            {

                Repeater1.DataSource = dtSerch;

                Repeater1.DataBind();

            }

            else

            {

                Repeater1.DataSource = dt;

                Repeater1.DataBind();

            }

            string str = DateTime.Now.ToString("yyyyMMddHHmmss");

            string str2 = "航线贴点分析报表(" + str + ")";

            Response.Clear();

            Response.Buffer = true;

            Response.Charset = "utf-8";

            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

            this.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(str2, System.Text.Encoding.UTF8) + ".xls");

            Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");     //设置输出流为简体中文

            this.Response.ContentType = "application/ms-excel";

            this.EnableViewState = false;

            StringWriter strWriter = new StringWriter();

            HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);

            htmlWriter.Write("<div align=center height=25 valign=middle><font size='6'>航线贴点分析报表</font></div><br>");



            this.Div1.RenderControl(htmlWriter);



            Response.Write(strWriter.ToString());

            Response.End();

        }

 

你可能感兴趣的:(导出Excel)