asp.net中GridView导出数据

 ///
        /// 重写VerifyRenderingInServerForm方法(不重写可能会报错)
        ///
        ///
        public override void VerifyRenderingInServerForm(Control control)
        {
        }
        ///
        /// 导出事件,调用导出方法
        ///
        ///
        ///
        protected void btnExport_Click(object sender, EventArgs e)
        {          
            Export("application/excel",fileName);
        }

 

 

 

    ///模式化特殊元素将服务控件改为Literal使其可以输出

        public void PrepareGridViewForExport(Control gv)  

      {
            LinkButton lb = new LinkButton();
            Literal l = new Literal();
            string name = String.Empty;

            for (int i = 0; i < gv.Controls.Count; i++)
            {

                if (gv.Controls[i].GetType() == typeof(LinkButton))
                {
                    l.Text = (gv.Controls[i] as LinkButton).Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(DropDownList))
                {
                    l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(CheckBox))
                {
                    l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                else if (gv.Controls[i].GetType() == typeof(ImageButton))
                {
                    l.Text = "图片";
                    gv.Controls.Remove(gv.Controls[i]);
                    gv.Controls.AddAt(i, l);
                }
                if (gv.Controls[i].HasControls())
                {
                    PrepareGridViewForExport(gv.Controls[i]);
                }
            }
        }
   

    ///导出方法

    ///FileType导出类型 application/excel导出excel文件word导出为word文档

    ///FileName 文件名称

        public void Export(string FileType, string FileName)
        {

            string style. = @"

            PrepareGridViewForExport(GridView1);//将模版列显示出来

 

            Response.Clear();

            Response.Charset = "GB2312 ";

            Response.ContentEncoding = Encoding.GetEncoding("GB2312");

            Response.Buffer = true;
 
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

            Response.ContentType = FileType;

            this.EnableViewState = false;

            this.GridView1.AllowPaging = false;

            CultureInfo myCItrad = new CultureInfo("ZH-CN", true);

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);

            this.GridView1.RenderControl(htw);

            Response.Write(style);

            Response.Write(sw.ToString());

            //Response.Write(dt.ToString());

            Response.End();

        }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-683049/,如需转载,请注明出处,否则将追究法律责任。

user_pic_default.png
请登录后发表评论 登录
全部评论
<%=items[i].createtime%>

<%=items[i].content%>

<%if(items[i].items.items.length) { %>
<%for(var j=0;j
<%=items[i].items.items[j].createtime%> 回复

<%=items[i].items.items[j].username%>   回复   <%=items[i].items.items[j].tousername%><%=items[i].items.items[j].content%>

<%}%> <%if(items[i].items.total > 5) { %>
还有<%=items[i].items.total-5%>条评论 ) data-count=1 data-flag=true>点击查看
<%}%>
<%}%> <%}%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值