代码改变世界

怎么样把GridView里的数据导出到Excel里

2009-03-14 16:28  苏飞  阅读(669)  评论(3编辑  收藏  举报

 

怎么样把DataView里的数据导出到Excel里
try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
            Response.Charset = "gb2312";
            Response.ContentType = "application/vnd.xls";//设置输出文件类型为excel文件。
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            GridView1.AllowPaging = false;
            GridView1.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
            GridView1.AllowPaging = true;
        }
        catch (Exception ee)
        {
            Response.Write(ee.Message.ToString());
        }