当前位置: 代码迷 >> .NET新技术 >> gridview导出到excel2007解决方案
  详细解决方案

gridview导出到excel2007解决方案

热度:285   发布时间:2016-04-25 01:25:27.0
gridview导出到excel2007
网上找了好多类似的帖子但是没有找到可以用的。希望大神答复。
------解决方案--------------------
LZ可以谷歌  SVN    关于导出execl的 资料
------解决方案--------------------
可以结贴给分了
 public class DataExport : System.Web.UI.Page
    {
        public void ExportToExcel(GridView gv, string FileName)
        {
            if (gv.Rows.Count > 0)
            {
                gv.BottomPagerRow.Visible = false;  //隐藏分页行
                //gv.BorderStyle = BorderStyle.Solid;
                //gv.BorderWidth = Unit.Pixel(1);

                PrepareControlForExport(gv.HeaderRow);
                gv.FooterRow.Visible = false;
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode((FileName + DateTime.Now.ToString("yyyyMMdd") + ".xls"), Encoding.UTF8).ToString());
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;
                StringWriter tw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                gv.RenderControl(hw);
                HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
                HttpContext.Current.Response.Write(tw.ToString());
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            else
            {
                HttpContext.Current.Response.Write("<script>alert('没有数据记录,无需导出!')</script>");
            }
        }

        public void ExportToExcel_NoDate(GridView gv, string FileName)
        {
            if (gv.Rows.Count > 0)
            {
                //gv.BottomPagerRow.Visible = false;  //隐藏分页行
                //gv.BorderStyle = BorderStyle.Solid;
                //gv.BorderWidth = Unit.Pixel(1);

                PrepareControlForExport(gv.HeaderRow);
                gv.FooterRow.Visible = false;
                HttpContext.Current.Response.Charset = "GB2312";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode((FileName + ".xls"), Encoding.UTF8).ToString());
                HttpContext.Current.Response.ContentType = "application/ms-excel";
  相关解决方案