博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
解决SharePoint中GridView导出Excel按钮的问题
阅读量:7200 次
发布时间:2019-06-29

本文共 3275 字,大约阅读时间需要 10 分钟。

大家都知道ASP.NET中GridView导出Excel的方法。在SharePoint中SPGridView是继承GridView的一个扩展控件,那么ASP.NET中的导出方法在SharePoint中也应适用。是可以用,但是有一个问题,就是第一次点击按钮导出成功后,你再次点击按钮的话,按钮就不在有用了。于是Google了一下,找到了这篇帖子解决了问题,就是在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);
        
}
    
}
}
分类: 

转载地址:http://vdzum.baihongyu.com/

你可能感兴趣的文章
CLLocation
查看>>
利用ansible modules模块来自定义集群管理
查看>>
跟我一起考PMP---项目成本管理
查看>>
一些数字货币(类比特币)介绍
查看>>
C#控件开发的闪烁问题和解决方法总结
查看>>
Java序列化高级认识
查看>>
Doubly linked list
查看>>
Android 自定义title样式
查看>>
Linux永久修改系统时间和时区方法
查看>>
HDU1257 最小拦截系统 【贪婪】
查看>>
Android 微信分享信息
查看>>
ECMAScript 6 Features 中文版
查看>>
Python 使用pymongo操作mongodb库
查看>>
sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询...
查看>>
eclipse快捷键
查看>>
javascript 常用对象
查看>>
浮躁的过去,开启的项目管理之路(二)
查看>>
java 遍历Map的四种方式
查看>>
blog推荐 - 左岸读书
查看>>
CentOS安装emacs24.2命令
查看>>