文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>解决SharePoint中GridView导出Excel按钮的问题

解决SharePoint中GridView导出Excel按钮的问题

时间:2010-09-07  来源:known

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

protected void Page_Load(object sender, EventArgs e)
{
    CreateToolBar();

    if (!IsPostBack)
    {
        string script = "_spOriginalFormAction = document.forms[0].action;\n_spSuppressFormOnSubmitWrapper = true;";
        this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", script, true);
    }
}

创建ToolBar方法:

private void CreateToolBar()
{
    ToolBar tb = (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);

    tb.Buttons.Controls.Add(btnExportToExcel);
    this.pnlToolbar.Controls.Clear();
    this.pnlToolbar.Controls.Add(tb);
}

导出按钮事件:

void btnExportToExcel_Click(object sender, EventArgs e)
{
    ExportToExcel("SearchActionItemResults", gvSearchResults);
}

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

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);
        }
    }
}
相关阅读 更多 +
排行榜 更多 +
终极街头格斗

终极街头格斗

休闲益智 下载
大炮轰飞机

大炮轰飞机

飞行射击 下载
像素打僵尸

像素打僵尸

飞行射击 下载