GridView 数据导出到 Excel文件

来源:互联网 发布:淘宝网手机电脑打不开 编辑:程序博客网 时间:2024/05/16 14:21

    protected void btnExportToExcel_Click(object sender, EventArgs e)
    {
        this.gvList.AllowPaging = false;
        BindGrid();

        string style = @"<style> .formatText { mso-number-format:/@; }</style> ";
        Response.ClearContent();

        Response.AddHeader("content-disposition", "attachment; filename=/"" + System.DateTime.Now.ToString("yyyy-MM-dd") + System.Web.HttpUtility.UrlEncode("一览表", System.Text.Encoding.UTF8) + ".xls/"");

        Response.ContentType = "application/excel";

        Response.Charset = "GB2312";
        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);

        gvList.RenderControl(htw);

        Response.Write(style);
        Response.Write(sw.ToString());

        Response.End();
    } 

原创粉丝点击