使用RenderControl方法把GridView控件导出到EXCEL

使用RenderControl方法把GridView控件导出到EXCEL// 把GridView输出到Excel文件       privatevoidExportExcel(GridViewgridView,stringtitle,stringtitle2,stringfileName)       {           intnHideCols=0;           //如果不想输出出某列,将Visible

大家好,又见面了,我是你们的朋友全栈君。

 //  把GridView输出到Excel文件
        private void ExportExcel(GridView gridView, string title, string title2, string fileName)
        {

            int nHideCols = 0;
            //如果不想输出出某列,将Visible设为false即可
            for (int i = 0; i < gridView.Columns.Count; i++)
            {

                if (gridView.Columns[i].HeaderText == “设备状态”)
                {

                    gridView.Columns[i].Visible = false;
                    gridView.Columns[i].ControlStyle.Width = 0;
                    nHideCols = 1;
                    break;
                }
            }
            //设定显示字符集
            Response.Charset = “utf-8”;
            //设定内容字符集
            Response.ContentEncoding = Encoding.GetEncoding(“utf-8”); 

           Asp.net中Response.Charset 与Response.ContentEncoding区别 


            //设定文件名
            Response.AppendHeader(“Content-Disposition”, “attachment;filename=” + HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace(‘+’, ‘_’).Replace(‘-‘, ‘_’));
            //设定文件类型   也可以是application/ms-word,也可以是text/html(字符集设为gb2312)
            Response.ContentType = “application/ms-excel”;
            this.EnableViewState = false;
            using (StringWriter tw = new StringWriter())
            {

                using (HtmlTextWriter hell = new HtmlTextWriter(tw))
                {

                    gridView.AllowPaging = false;
                    gridView.RenderControl(hell);

                    string s = tw.ToString();
                    s = s.Replace(“\r\n”, “”);
                    int index = s.IndexOf(“<tr”);
                    //可以自定义Excel文件的标题
                    string head = “<tr><td colspan=\”” + (gridView.Columns.Count – nHideCols).ToString() + “\” style=\”text-align: center; height: 42px; font-size: 24px; font-weight: bolder; color: #000000;\”>” + title + “</td></tr>” +
                            “<tr><td colspan=\”” + (gridView.Columns.Count – nHideCols).ToString() + “\” style=\”text-align: center; height: 24px; font-size: 12px; color: #000000;\”>” + title2 + “</td></tr>”;
                    //使用Index来判断是否存在数据,当然也可以用gridView.Rows.Count来判断
                    if (index != -1)
                    {

                        //有数据的
                        s = s.Insert(index, head);
                    }
                    else
                    {

                        //没有数据的时候
                        s = “<table cellspacing=\”0\” cellpadding=\”3\” rules=\”rows\” border=\”1\” id=\”” + gridView.ID + “\” style=\”background-color:White;border-color:#E7E7FF;border-width:1px;border-style:None;border-collapse:collapse;\”>” +
                            head + “</table>”;
                    }
                    Response.Write(s);
                    Response.End();
                }
            }
        }

        //同时vs2005,vs2003会报错“类型“ExGridView”的控件“GridViewMaster”必须放在具有 runat=server 的窗体标记内
        //需要添加下面取消对GridViewMaster 控件验证的方法
        public override void VerifyRenderingInServerForm(Control control)
        {

            if (!control.GetType().Equals(gridView.GetType()))
            {

                base.VerifyRenderingInServerForm(control);
            }
        }

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162030.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月20日 下午6:16
下一篇 2022年7月20日 下午6:16


相关推荐

  • tensorflow estimator使用总结

    tensorflow estimator使用总结最近在使用 estimator 做项目 发现官网对这个 estimator 整体的讲解和使用过程中的细节讲的比较少 结合我是用过程中的问题 对 estimator 的使用步骤进行了总结 如下 代码见 github 求 star 1 estimator 主要需要 model fn input fn 以及 serving fn2 model fn 主要是是用来定义 model input

    2025年8月14日
    9
  • 小议隐式转换引起的问题

    小议隐式转换引起的问题

    2021年11月28日
    165
  • pycharm 2021  激活码【在线注册码/序列号/破解码】[通俗易懂]

    pycharm 2021  激活码【在线注册码/序列号/破解码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    62
  • MD5算法如何被激活成功教程

    MD5算法如何被激活成功教程小明 老师 上次您讲了 MD5 算法 用它生成的信息摘要 真的可以被激活成功教程吗 老师 有很多种方法可以激活成功教程 不过需要明确一点 这里所谓的激活成功教程 并非把摘要还原成原文 为什么呢 因为固定 128 位的摘要是有穷的 而原文数量是无穷的 每一个摘要都可以由若干个原文通过 Hash 得到 小明 如果是这样的话 网上所说的 MD5 激活成功教程到底是怎么回事呢 老师 对于 MD5 的激活成功教程 实际上都属于 碰撞 比如原文 A 通过 MD

    2026年3月17日
    2
  • Windows更新清理工具 (winsxs 清理工具)

    Windows更新清理工具 (winsxs 清理工具)Windows 更新清理工具是一款效果非常显著的 Windows7 Windows8 操作系统清理优化工具 经常安装系统的朋友相比有所体会 刚刚安装完成的 Win7 Win8 其实占的空间并不大 去掉页面文件和休眠文件后 真正的系统只几个 G 而已 但如果你给系统升级了补丁后 你会发现系统所需要占用的空间直线上升 轻松突破 10G 的门槛 加上休眠文件和页面文件 本来预留给系统的 30G 空间马上变得也不富裕了

    2026年3月17日
    2
  • PHPstorm相关设置以及快捷键

    PHPstorm相关设置以及快捷键

    2021年9月18日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号