Sharepoint自定义Webpart网格数据导出到Excel [英] Sharepoint custom webpart Grid data to export to excel

查看:63
本文介绍了Sharepoint自定义Webpart网格数据导出到Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.

在一个应用程序中,我有一个报告页面,该页面基于一些过滤器在网格中显示数据.

I an application in which I have one reports page where I am showing data in grid based on some filters.

我想将过滤后的网格数据导出到excel.

I want to export the filtered gird data to excel.

我已经尝试了很多来自网络的代码,但是每个代码都给出了一些错误.

I have tried so many code from net but each one is giving some error.

有一种代码对我有用,但是它可以在excel文件中获取页面的所有内容,而不仅仅是网格数据.

One code is working for me but it is getting all the content of the page in the excel file instead of only grid data.

以下是我的代码.

     受保护的无效btnExport_Click(对象发送者,EventArgs e)
       {
          试试
           {

       protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {

               GridView1.AllowPaging = false;

                GridView1.AllowPaging = false;

               this.PopulateGrid(); //正在从数据库中获取数据并显示在网格中

                this.PopulateGrid(); //is getting data from the database and showing in grid

               HttpContext.Current.Response.Clear();

                HttpContext.Current.Response.Clear();

               HttpContext.Current.Response.Buffer = true;

                HttpContext.Current.Response.Buffer = true;

               HttpContext.Current.Response.AddHeader("content-disposition",

                HttpContext.Current.Response.AddHeader("content-disposition",

                   " attachment; filename = Report.xls");

                 "attachment;filename=Report.xls");

               HttpContext.Current.Response.Charset =";

                HttpContext.Current.Response.Charset = "";

               HttpContext.Current.Response.ContentType ="application/vnd.ms-excel";

                HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

               StringWriter sw = new StringWriter();

                StringWriter sw = new StringWriter();

               HtmlTextWriter hw =新的HtmlTextWriter(sw);

                HtmlTextWriter hw = new HtmlTextWriter(sw);

               for(int i = 0; i&GridView1.Rows.Count; i ++)
                             {

                for (int i = 0; i < GridView1.Rows.Count; i++)
                {

              //将文本样式应用于每一行

                    //Apply text style to each Row

              GridView1.Rows [i] .Attributes.Add("class","textmode");

                    GridView1.Rows[i].Attributes.Add("class", "textmode");

               }

                }

               GridView1.RenderControl(hw);

                GridView1.RenderControl(hw);

               //将数字格式化为字符串的样式

                //style to format numbers to string

              字符串样式= @"< style> .textmode {mso-number-format:\ @; }</style>" ;;

                string style = @"<style> .textmode { mso-number-format:\@; } </style>";

               HttpContext.Current.Response.Write(style);

                HttpContext.Current.Response.Write(style);

               HttpContext.Current.Response.Output.Write(sw.ToString());

                HttpContext.Current.Response.Output.Write(sw.ToString());

               HttpContext.Current.Response.Flush();

                HttpContext.Current.Response.Flush();

               HttpContext.Current.Response.End();

                HttpContext.Current.Response.End();

               //}

                //}

           }
           catch(异常例外)
           {
                             AddError(" vwpReport:ExportToExcel:" + ex.Message);
           }

            }
            catch (Exception ex)
            {
                AddError("vwpReport:ExportToExcel:" + ex.Message);
            }

       }

        }

      私有静态无效PrepareControlForExport(Control control)
       {
           for(int i = 0; i< control.Controls.Count; i ++)
           {
                            控制电流= control.Controls [i];
                            如果(当前为System.Web.UI.WebControls.Image)
                             {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i,new LiteralControl((当前为System.Web.UI.WebControls.Image).AlternateText));
                             }
                            如果(当前是LinkBut​​ton)
                             {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i,new LiteralControl((当前为LinkBut​​ton).Text));
                             }
                            否则,如果(当前为ImageButton)
                             {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i,new LiteralControl((当前为ImageButton).AlternateText));
                             }
                            否则,如果(当前为HyperLink)
                             {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i,new LiteralControl((当前为HyperLink).Text));
                             }
                            否则,如果(当前是DropDownList)
                             {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i,new LiteralControl((当前为DropDownList).SelectedItem.Text));
                             }
                            否则,如果(当前为CheckBox)
                             {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i,new LiteralControl((当前为CheckBox).已检查?"True":"False")));
                             }

        private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is System.Web.UI.WebControls.Image)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as System.Web.UI.WebControls.Image).AlternateText));
                }
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }


                            如果(current.HasControls())
                             {
                    PrepareControlForExport(当前);
                             }
           }
       }


                if (current.HasControls())
                {
                    PrepareControlForExport(current);
                }
            }
        }

任何人都可以帮助我解决Excel中的问题,以便它仅显示网格数据,而不显示完整的页面详细信息,例如下拉列表过滤任何文本等.

Can any one help me to fix the issue in Excel so that it will display only the grid data not the full page details like dropdown filters any text etc.

谢谢

推荐答案

您可以尝试下面的代码.

    private void ExportGridToExcel()  
    {  
        Response.Clear();  
        Response.Buffer = true;  
        Response.ClearContent();  
        Response.ClearHeaders();  
        Response.Charset = "";  
        string FileName ="Test"+DateTime.Now+".xls";  
        StringWriter strwritter = new StringWriter();  
        HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);        
        Response.Cache.SetCacheability(HttpCacheability.NoCache);  
        Response.ContentType ="application/vnd.ms-excel";    
        Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);  
        GridView1.GridLines = GridLines.Both;  
        GridView1.HeaderStyle.Font.Bold = true;  
        GridView1.RenderControl(htmltextwrtter);  
        Response.Write(strwritter.ToString());  
        Response.End();      
  
    }

最好的问候,

Lee


这篇关于Sharepoint自定义Webpart网格数据导出到Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆