asp.net代码收集(1)
动态生成datagrid并导出到Excel或Wrod。
一、动态生成DataGrid
二、导出数据
1
string lsFileName = string.Empty;
2
string lsOwner = string.Empty;
3
DataGrid dg = new DataGrid();
4
[动态创建Grid]#region [动态创建Grid]
5
dg.AutoGenerateColumns = false;
6
BoundColumn bc = new BoundColumn();
7
bc.DataField = "CommentType";
8
bc.HeaderText = "类型";
9
dg.Columns.Add(bc);
10
11
bc = new BoundColumn();
12
bc.DataField = "DocPath";
13
bc.HeaderText = "文档路径";
14
dg.Columns.Add(bc);
15
16
bc = new BoundColumn();
17
bc.DataField = "AuthorCode";
18
bc.HeaderText = "工号";
19
dg.Columns.Add(bc);
20
21
bc = new BoundColumn();
22
bc.DataField = "AuthorName";
23
bc.HeaderText = "姓名";
24
dg.Columns.Add(bc);
25
26
bc = new BoundColumn();
27
bc.DataField = "CommentContent";
28
bc.HeaderText = "内容";
29
dg.Columns.Add(bc);
30
31
bc = new BoundColumn();
32
bc.DataField = "CreateDate";
33
bc.HeaderText = "发表时间";
34
dg.Columns.Add(bc);
35
#endregion [动态创建Grid]
36
[获取数据源 ]#region [获取数据源 ]
37
DataSet ds = null;
38
string lsDeptRowID,lsAuthor,lsBeginDate,lsEndDate;
39
int liPlatform,liBrand,liOnlyReplied;
40
Hashtable ht = (Hashtable)Session["ExportData"];
41
lsDeptRowID = ht["CommentQuery_DeptRowID"].ToString();
42
lsAuthor = ht["CommentQuery_Author"].ToString();
43
lsBeginDate = ht["CommentQuery_BeginDate"].ToString();
44
lsEndDate = ht["CommentQuery_EndDate"].ToString();
45
liPlatform = Convert.ToInt32(ht["CommentQuery_Platform"]);
46
liBrand = Convert.ToInt32(ht["CommentQuery_Brand"]);
47
liOnlyReplied = Convert.ToInt32(ht["CommentQuery_OnlyReplied"]);
48
Comment cmt = new Comment();
49
ds = cmt.CommentExportByQuery(lsDeptRowID,liPlatform,liBrand,lsAuthor,lsBeginDate,lsEndDate,liOnlyReplied,pbIsDelete);
50
51
dg.DataSource = ds.Tables[0].DefaultView;
52
dg.DataBind();
53
#endregion [获取数据源 ]
string lsFileName = string.Empty;2
string lsOwner = string.Empty;3
DataGrid dg = new DataGrid();4

[动态创建Grid]#region [动态创建Grid] 5
dg.AutoGenerateColumns = false;6
BoundColumn bc = new BoundColumn();7
bc.DataField = "CommentType";8
bc.HeaderText = "类型";9
dg.Columns.Add(bc);10

11
bc = new BoundColumn();12
bc.DataField = "DocPath";13
bc.HeaderText = "文档路径";14
dg.Columns.Add(bc);15
16
bc = new BoundColumn();17
bc.DataField = "AuthorCode";18
bc.HeaderText = "工号";19
dg.Columns.Add(bc);20

21
bc = new BoundColumn();22
bc.DataField = "AuthorName";23
bc.HeaderText = "姓名";24
dg.Columns.Add(bc);25

26
bc = new BoundColumn();27
bc.DataField = "CommentContent";28
bc.HeaderText = "内容";29
dg.Columns.Add(bc);30

31
bc = new BoundColumn();32
bc.DataField = "CreateDate";33
bc.HeaderText = "发表时间";34
dg.Columns.Add(bc);35
#endregion [动态创建Grid]36

[获取数据源 ]#region [获取数据源 ]37
DataSet ds = null;38
string lsDeptRowID,lsAuthor,lsBeginDate,lsEndDate;39
int liPlatform,liBrand,liOnlyReplied; 40
Hashtable ht = (Hashtable)Session["ExportData"];41
lsDeptRowID = ht["CommentQuery_DeptRowID"].ToString();42
lsAuthor = ht["CommentQuery_Author"].ToString();43
lsBeginDate = ht["CommentQuery_BeginDate"].ToString();44
lsEndDate = ht["CommentQuery_EndDate"].ToString();45
liPlatform = Convert.ToInt32(ht["CommentQuery_Platform"]);46
liBrand = Convert.ToInt32(ht["CommentQuery_Brand"]);47
liOnlyReplied = Convert.ToInt32(ht["CommentQuery_OnlyReplied"]);48
Comment cmt = new Comment();49
ds = cmt.CommentExportByQuery(lsDeptRowID,liPlatform,liBrand,lsAuthor,lsBeginDate,lsEndDate,liOnlyReplied,pbIsDelete); 50

51
dg.DataSource = ds.Tables[0].DefaultView;52
dg.DataBind();53
#endregion [获取数据源 ]二、导出数据
1
[导出]#region [导出]
2
//设置输出流的 HTTP MIME 类型(默认值为“text/html”)
3
// Response.ContentType = "application/vnd.ms-excel";
4
Response.ContentType = this.dlExportType.SelectedValue;
5
Response.Charset = "utf-8";
6
//设置文件名
7
lsFileName = "CommentQuery"+DateTime.Today.Year.ToString()+DateTime.Today.Month.ToString()+DateTime.Today.Day.ToString();
8
if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-excel"))
9
lsFileName = lsFileName+".xls";
10
else if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-word"))
11
lsFileName = lsFileName+".doc";
12
lsFileName = HttpUtility.UrlEncode(lsFileName,System.Text.Encoding.GetEncoding("utf-8"));
13
Response.AddHeader("Content-Disposition", "attachment; filename="+lsFileName);
14
15
this.EnableViewState=false;
16
System.IO.StringWriter sw = new System.IO.StringWriter();
17
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
18
//把服务器控件DataGrid的内容呈现给指定的HtmlTextWriter对象hw
19
dg.RenderControl(hw);
20
//将字符串写入 HTTP 输出内容流
21
Response.Write(sw.ToString());
22
//将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 Application_EndRequest 事件
23
Response.End();
24
#endregion [导出]

[导出]#region [导出]2
//设置输出流的 HTTP MIME 类型(默认值为“text/html”)3
// Response.ContentType = "application/vnd.ms-excel";4
Response.ContentType = this.dlExportType.SelectedValue;5
Response.Charset = "utf-8";6
//设置文件名7
lsFileName = "CommentQuery"+DateTime.Today.Year.ToString()+DateTime.Today.Month.ToString()+DateTime.Today.Day.ToString();8
if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-excel"))9
lsFileName = lsFileName+".xls";10
else if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-word"))11
lsFileName = lsFileName+".doc";12
lsFileName = HttpUtility.UrlEncode(lsFileName,System.Text.Encoding.GetEncoding("utf-8"));13
Response.AddHeader("Content-Disposition", "attachment; filename="+lsFileName);14
15
this.EnableViewState=false;16
System.IO.StringWriter sw = new System.IO.StringWriter();17
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw); 18
//把服务器控件DataGrid的内容呈现给指定的HtmlTextWriter对象hw19
dg.RenderControl(hw);20
//将字符串写入 HTTP 输出内容流21
Response.Write(sw.ToString());22
//将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 Application_EndRequest 事件23
Response.End();24
#endregion [导出]

浙公网安备 33010602011771号