解决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
protected void Page_Load(object sender, EventArgs e)
2
{
3
this.CreateToolBar();
4

5
string script = "_spOriginalFormAction = document.forms[0].action;\n_spSuppressFormOnSubmitWrapper = true;";
6
this.ClientScript.RegisterClientScriptBlock(this.GetType(), "script", script, true);
7
}
创建ToolBar方法:
01
private void CreateToolBar()
02
{
03
ToolBar toolBar = (ToolBar)Page.LoadControl("~/_controltemplates/ToolBar.ascx");
04

05
ToolBarButton btnExportToExcel = (ToolBarButton)Page.LoadControl("~/_controltemplates/ToolBarButton.ascx");
06
btnExportToExcel.ID = "btnExportToExcel";
07
btnExportToExcel.Text = "Export to Spreadsheet";
08
btnExportToExcel.ImageUrl = "/_layouts/images/icxls.gif";
09
btnExportToExcel.Click += new EventHandler(btnExportToExcel_Click);
10

11
toolBar.Buttons.Controls.Add(btnExportToExcel);
12
this.Toolbar.Controls.Clear();
13
this.Toolbar.Controls.Add(toolBar);
14
}
导出按钮事件:
1
void btnExportToExcel_Click(object sender, EventArgs e)
2
{
3
ExportToExcel("SearchResults", gvSearchResults);
4
}
下面是SharePoint中导出Excel的完整代码:
view sourceprint?
01
protected void ExportToExcel(string fileName, GridView gv)
02
{
03
Response.Clear();
04
Response.Charset = "GB2312";
05
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
06
Response.ContentType = "application/ms-excel";
07

08
using (TextWriter tw = new StringWriter())
09
{
10
using (HtmlTextWriter htw = new HtmlTextWriter(tw))
11
{
12
Table table = new Table();
13
table.GridLines = gv.GridLines;
14

15
if (gv.HeaderRow != null)
16
{
17
PrepareControlForExport(gv.HeaderRow);
18
table.Rows.Add(gv.HeaderRow);
19
}
20

21
foreach (GridViewRow row in gv.Rows)
22
{
23
PrepareControlForExport(row);
24
table.Rows.Add(row);
25
}
26

27
if (gv.FooterRow != null)
28
{
29
PrepareControlForExport(gv.FooterRow);
30
table.Rows.Add(gv.FooterRow);
31
}
32

33
table.RenderControl(htw);
34

35
Response.Write(tw.ToString());
36
Response.End();
37
}
38
}
39
}
40

41
private void PrepareControlForExport(Control control)
42
{
43
for (int i = 0; i < control.Controls.Count; i++)
44
{
45
Control current = control.Controls[i];
46

47
if (current is LinkButton)
48
{
49
control.Controls.Remove(current);
50
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
51
}
52
else if (current is ImageButton)
53
{
54
control.Controls.Remove(current);
55
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
56
}
57
else if (current is HyperLink)
58
{
59
control.Controls.Remove(current);
60
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
61
}
62
else if (current is DropDownList)
63
{
64
control.Controls.Remove(current);
65
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
66
}
67
else if (current is CheckBox)
68
{
69
control.Controls.Remove(current);
70
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
71
}
72

73
if (current.HasControls())
74
{
75
PrepareControlForExport(current);
76
}
77
}
78
}

原文地址:https://www.cnblogs.com/Areas/p/2267230.html