解决SharePoint中GridView导出Excel按钮的问题
大家都知道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) { this.CreateToolBar(); string script = "_spOriginalFormAction = document.forms[0].action;\n_spSuppressFormOnSubmitWrapper = true;"; this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", script, true); }
创建ToolBar方法:
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); }
导出按钮事件:
void btnExportToExcel_Click(object sender, EventArgs e) { ExportToExcel("SearchResults", 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); } } }