玄铁剑

成功的途径:抄,创造,研究,发明...
posts - 128, comments - 42, trackbacks - 0, articles - 174

导航

<2008年12月>
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

常用链接

留言簿(5)

随笔分类

随笔档案

文章分类

收藏夹

AJAX

asp.net網絡鏈接

Delphi Link

IronPython

Microsoft

Other Blogs

OtherSite

SliverLight

Test

免费asp.net控件

其它链接

搜索

  •  

积分与排名

  • 积分 - 217459
  • 排名 - 26

最新评论

阅读排行榜

评论排行榜

GridView下载成Excel(IE6/IE7正常显示)

Posted on 2008-12-22 20:27 玄铁剑 阅读(572) 评论(0)  编辑 收藏 引用 所属分类: asp.net
        //public override void VerifyRenderingInServerForm(Control control)
        //{
        //}
        static public void DownLoadFile(GridView gvToExcel, string strTrueFileName)
        {
            HttpServerUtility CurrentServer = HttpContext.Current.Server;
            HttpResponse CurrentResponse = HttpContext.Current.Response;
            string strPath = UDF.GetAppSettingValue(UdfType.TempOutputPath);
            string strFile = CurrentServer.MapPath(strPath) + strTrueFileName;
            FileInfo file;
            if (File.Exists(strFile))
            {
                file = new FileInfo(strFile);
                file.IsReadOnly = false;
                file.Delete();
            }
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            gvToExcel.RenderControl(htmlWrite);
            FileStream fs = File.Create(strFile);
            byte[] info = new UTF8Encoding(true).GetBytes(stringWrite.ToString());
            fs.Write(info, 0, info.Length);
            fs.Close();
            file = new FileInfo(strFile);
            CurrentResponse.Clear();
            CurrentResponse.ClearHeaders();
            CurrentResponse.Buffer = false;
            CurrentResponse.AddHeader("Content-Disposition", "attachment;filename=" + CurrentServer.UrlEncode(strTrueFileName));
            CurrentResponse.AddHeader("Content-Length", file.Length.ToString());
            CurrentResponse.ContentType = "application/octet-stream";//saveAs
            CurrentResponse.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
            CurrentResponse.WriteFile(strFile);
            CurrentResponse.Flush();
            CurrentResponse.End();
        }
只有注册用户登录后才能发表评论。