ASP.NET 导出数据表格

功能:可以实现导出整个数据表格或整个页面

 public bool ExportGv(string fileType, string fileName)
        {
            bool flag = false;
            try
            {
                //定义文档类型、字符编码
                Response.Clear();
                Response.Buffer = true;
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

                //2.定义导入文档的类型
                HttpContext.Current.Response.ContentType = fileType;
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +
                    System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                this.Gv.Page.EnableViewState = false;

                //定义一个输入流
                System.IO.StringWriter tw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);

                //将目标数据绑定到输入流输出,这里的this可以换成要导出的控件数据
                this.RenderControl(hw);
                Response.Output.Write(tw.ToString());
                Response.Flush();
                Response.End();
                this.Gv.AllowPaging = false;
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
                throw ex;
            }
            return flag;
        }


        有时候导出时出现缺少CSS样式的特性,自此补充
         public bool exportAndPrint(string fileType, string fileName)
        {
            bool flag = false;
            try
            {
                //定义文档类型、字符编码
                Response.Clear();
                Response.Buffer = true;
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

                //2.定义导入文档的类型
                HttpContext.Current.Response.ContentType = fileType;
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=\"" +
                    System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

                //定义一个输入流
                System.IO.StringWriter tw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);

                //将目标数据绑定到输入流输出
                this.RenderControl(hw);


                string outStr = "<style>   ";
                using (System.IO.StreamReader sr = new System.IO.StreamReader(Server.MapPath("~/CSS/APDetailInfo.css")))
                {
                    outStr += sr.ReadToEnd();
                }
            
                outStr += "  </style>";
                outStr = outStr.Replace("/r/n", "");

                outStr = tw.ToString().Replace("<link href=\"CSS/APDetailInfo.css\" rel=\"stylesheet\" />", "");
                Response.Output.Write(outStr);
                Response.Flush();
                Response.End();
                flag = true;
            }
            catch (Exception ex)
            {
                flag = false;
                throw ex;
            }
            return flag;
        }


        protected void Export_Click(object sender, EventArgs e)
        {

            TYKY_OA.Web.Listnation<TYKY_OA.Model.ExtraInfo> pageList = (Listnation<Model.ExtraInfo>)Session["ADAFOTRlistnation"];
            if (pageList != null)
            {

               //由于是导出整个页面,有些控件不想被导出,所以置为空
                this.Gv.Columns[10].Visible = false;

 

                 //这里是整个DIV的隐藏
                this.head.Visible = false;
                this.boot.Visible = false;

                this.Gv.DataSource = pageList.ObjList;
                this.Gv.DataBind();
                //ms-word/ms-txt/ms-html/ms-excel
                if (!ExportGv("application/ms-excel", "ExportGv.xls"))
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "ExportFailed", "<script>alert('数据导入失败');</script>");
                }
                this.prev.Disabled = !pageList.FlagPrivous;
                this.next.Disabled = !pageList.FlagNext;
                this.pageIndex.Text = "当前第" + pageList.CurrentPage + "页" + "(共" + pageList.PageCount + "页)";
                this.Gv.DataSource = pageList.GetList();
                this.Gv.DataBind();
            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {
           // base.VerifyRenderingInServerForm(control);
        }

posted @ 2014-01-27 16:11  aswater  阅读(635)  评论(0编辑  收藏  举报