ITPub博客

首页 > Linux操作系统 > Linux操作系统 > asp.net中GridView导出数据

asp.net中GridView导出数据

原创 Linux操作系统 作者:iDotNetSpace 时间:2010-12-31 14:15:38 0 删除 编辑
 ///
        /// 重写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. = @"