zoukankan      html  css  js  c++  java
  • 解决SharePoint中GridView导出Excel按钮的问题

    大家都知道ASP.NET中GridView导出Excel的方法。在SharePoint中SPGridView是继承GridView的一个扩展控件,那么ASP.NET中的导出方法在SharePoint中也应适用。是可以用,但是有一个问题,就是第一次点击按钮导出成功后,你再次点击按钮的话,按钮就不在有用了。于是Google了一下,找到了这篇Export GridView to Excel in web part帖子解决了问题,就是在Page_Load中注册两行Javascript脚本。

    1
    2
    3
    4
    5
    6
    7
    protected void Page_Load(object sender, EventArgs e)
    {
        this.CreateToolBar();
     
        string script = "_spOriginalFormAction = document.forms[0].action;\n_spSuppressFormOnSubmitWrapper = true;";
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", script, true);
    }

    创建ToolBar方法:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    private void CreateToolBar()
    {
        ToolBar toolBar = (ToolBar)Page.LoadControl("~/_controltemplates/ToolBar.ascx");
     
        ToolBarButton btnExportToExcel = (ToolBarButton)Page.LoadControl("~/_controltemplates/ToolBarButton.ascx");
        btnExportToExcel.ID = "btnExportToExcel";
        btnExportToExcel.Text = "Export to Spreadsheet";
        btnExportToExcel.ImageUrl = "/_layouts/images/icxls.gif";
        btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
     
        toolBar.Buttons.Controls.Add(btnExportToExcel);
        this.Toolbar.Controls.Clear();
        this.Toolbar.Controls.Add(toolBar);
    }

    导出按钮事件:

    1
    2
    3
    4
    void btnExportToExcel_Click(object sender, EventArgs e)
    {
        ExportToExcel("SearchResults", gvSearchResults);
    }

    下面是SharePoint中导出Excel的完整代码:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    protected void ExportToExcel(string fileName, GridView gv)
    {
        Response.Clear();
        Response.Charset = "GB2312";
        Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        Response.ContentType = "application/ms-excel";
     
        using (TextWriter tw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(tw))
            {
                Table table = new Table();
                table.GridLines = gv.GridLines;
     
                if (gv.HeaderRow != null)
                {
                    PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }
     
                foreach (GridViewRow row in gv.Rows)
                {
                    PrepareControlForExport(row);
                    table.Rows.Add(row);
                }
     
                if (gv.FooterRow != null)
                {
                    PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }
     
                table.RenderControl(htw);
     
                Response.Write(tw.ToString());
                Response.End();
            }
        }
    }
     
    private void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
     
            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"));
            }
     
            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }
    分类: MOSS
  • 相关阅读:
    TensorFlow基础篇
    MySql分类
    VISUAL STUDIO 调试
    排序分类
    位分类
    Visio分类
    工作线程AfxBeginThread的使用
    Windows Live Writer配置步骤
    用户界面线程AfxBeginThread的使用
    WIndows 相关知识
  • 原文地址:https://www.cnblogs.com/Areas/p/2303736.html
Copyright © 2011-2022 走看看