C#将GridView中的数据导出为excel文件

protected void exportBtn_Click(object sender, EventArgs e)
    {
        Response.Clear();   //清除缓冲区流中的所有内容输出
        Response.AppendHeader("Content-Disposition", "attachment;filename=SatffInformation.xls");//将HTTP头添加到输出流
        //设置输出流的编码方式
        Response.Charset = "GB2312";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding(Response.Charset);
        //设置输出文件类型
        Response.ContentType = "application/ms-excel";
        //数据写入
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        InfoGV.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.Flush();
        Response.End();
            
    }
    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
            
    }

C#将GridView中的数据导出为excel文件_第1张图片

 

你可能感兴趣的:(c#,excel,ui)