asp.net代码收集(1)
动态生成datagrid并导出到Excel或Wrod。
一、动态生成DataGrid
二、导出数据
1string lsFileName = string.Empty;
2string lsOwner = string.Empty;
3DataGrid dg = new DataGrid();
4[动态创建Grid]#region [动态创建Grid]
5dg.AutoGenerateColumns = false;
6BoundColumn bc = new BoundColumn();
7bc.DataField = "CommentType";
8bc.HeaderText = "类型";
9dg.Columns.Add(bc);
10
11bc = new BoundColumn();
12bc.DataField = "DocPath";
13bc.HeaderText = "文档路径";
14dg.Columns.Add(bc);
15
16bc = new BoundColumn();
17bc.DataField = "AuthorCode";
18bc.HeaderText = "工号";
19dg.Columns.Add(bc);
20
21bc = new BoundColumn();
22bc.DataField = "AuthorName";
23bc.HeaderText = "姓名";
24dg.Columns.Add(bc);
25
26bc = new BoundColumn();
27bc.DataField = "CommentContent";
28bc.HeaderText = "内容";
29dg.Columns.Add(bc);
30
31bc = new BoundColumn();
32bc.DataField = "CreateDate";
33bc.HeaderText = "发表时间";
34dg.Columns.Add(bc);
35#endregion [动态创建Grid]
36[获取数据源 ]#region [获取数据源 ]
37DataSet ds = null;
38string lsDeptRowID,lsAuthor,lsBeginDate,lsEndDate;
39int liPlatform,liBrand,liOnlyReplied;
40Hashtable ht = (Hashtable)Session["ExportData"];
41lsDeptRowID = ht["CommentQuery_DeptRowID"].ToString();
42lsAuthor = ht["CommentQuery_Author"].ToString();
43lsBeginDate = ht["CommentQuery_BeginDate"].ToString();
44lsEndDate = ht["CommentQuery_EndDate"].ToString();
45liPlatform = Convert.ToInt32(ht["CommentQuery_Platform"]);
46liBrand = Convert.ToInt32(ht["CommentQuery_Brand"]);
47liOnlyReplied = Convert.ToInt32(ht["CommentQuery_OnlyReplied"]);
48Comment cmt = new Comment();
49ds = cmt.CommentExportByQuery(lsDeptRowID,liPlatform,liBrand,lsAuthor,lsBeginDate,lsEndDate,liOnlyReplied,pbIsDelete);
50
51dg.DataSource = ds.Tables[0].DefaultView;
52dg.DataBind();
53#endregion [获取数据源 ]
2string lsOwner = string.Empty;
3DataGrid dg = new DataGrid();
4[动态创建Grid]#region [动态创建Grid]
5dg.AutoGenerateColumns = false;
6BoundColumn bc = new BoundColumn();
7bc.DataField = "CommentType";
8bc.HeaderText = "类型";
9dg.Columns.Add(bc);
10
11bc = new BoundColumn();
12bc.DataField = "DocPath";
13bc.HeaderText = "文档路径";
14dg.Columns.Add(bc);
15
16bc = new BoundColumn();
17bc.DataField = "AuthorCode";
18bc.HeaderText = "工号";
19dg.Columns.Add(bc);
20
21bc = new BoundColumn();
22bc.DataField = "AuthorName";
23bc.HeaderText = "姓名";
24dg.Columns.Add(bc);
25
26bc = new BoundColumn();
27bc.DataField = "CommentContent";
28bc.HeaderText = "内容";
29dg.Columns.Add(bc);
30
31bc = new BoundColumn();
32bc.DataField = "CreateDate";
33bc.HeaderText = "发表时间";
34dg.Columns.Add(bc);
35#endregion [动态创建Grid]
36[获取数据源 ]#region [获取数据源 ]
37DataSet ds = null;
38string lsDeptRowID,lsAuthor,lsBeginDate,lsEndDate;
39int liPlatform,liBrand,liOnlyReplied;
40Hashtable ht = (Hashtable)Session["ExportData"];
41lsDeptRowID = ht["CommentQuery_DeptRowID"].ToString();
42lsAuthor = ht["CommentQuery_Author"].ToString();
43lsBeginDate = ht["CommentQuery_BeginDate"].ToString();
44lsEndDate = ht["CommentQuery_EndDate"].ToString();
45liPlatform = Convert.ToInt32(ht["CommentQuery_Platform"]);
46liBrand = Convert.ToInt32(ht["CommentQuery_Brand"]);
47liOnlyReplied = Convert.ToInt32(ht["CommentQuery_OnlyReplied"]);
48Comment cmt = new Comment();
49ds = cmt.CommentExportByQuery(lsDeptRowID,liPlatform,liBrand,lsAuthor,lsBeginDate,lsEndDate,liOnlyReplied,pbIsDelete);
50
51dg.DataSource = ds.Tables[0].DefaultView;
52dg.DataBind();
53#endregion [获取数据源 ]
二、导出数据
1[导出]#region [导出]
2//设置输出流的 HTTP MIME 类型(默认值为“text/html”)
3// Response.ContentType = "application/vnd.ms-excel";
4Response.ContentType = this.dlExportType.SelectedValue;
5Response.Charset = "utf-8";
6//设置文件名
7lsFileName = "CommentQuery"+DateTime.Today.Year.ToString()+DateTime.Today.Month.ToString()+DateTime.Today.Day.ToString();
8if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-excel"))
9 lsFileName = lsFileName+".xls";
10else if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-word"))
11 lsFileName = lsFileName+".doc";
12lsFileName = HttpUtility.UrlEncode(lsFileName,System.Text.Encoding.GetEncoding("utf-8"));
13Response.AddHeader("Content-Disposition", "attachment; filename="+lsFileName);
14
15this.EnableViewState=false;
16System.IO.StringWriter sw = new System.IO.StringWriter();
17System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
18//把服务器控件DataGrid的内容呈现给指定的HtmlTextWriter对象hw
19dg.RenderControl(hw);
20//将字符串写入 HTTP 输出内容流
21Response.Write(sw.ToString());
22//将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 Application_EndRequest 事件
23Response.End();
24#endregion [导出]
2//设置输出流的 HTTP MIME 类型(默认值为“text/html”)
3// Response.ContentType = "application/vnd.ms-excel";
4Response.ContentType = this.dlExportType.SelectedValue;
5Response.Charset = "utf-8";
6//设置文件名
7lsFileName = "CommentQuery"+DateTime.Today.Year.ToString()+DateTime.Today.Month.ToString()+DateTime.Today.Day.ToString();
8if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-excel"))
9 lsFileName = lsFileName+".xls";
10else if(this.dlExportType.SelectedValue.Equals("application/vnd.ms-word"))
11 lsFileName = lsFileName+".doc";
12lsFileName = HttpUtility.UrlEncode(lsFileName,System.Text.Encoding.GetEncoding("utf-8"));
13Response.AddHeader("Content-Disposition", "attachment; filename="+lsFileName);
14
15this.EnableViewState=false;
16System.IO.StringWriter sw = new System.IO.StringWriter();
17System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
18//把服务器控件DataGrid的内容呈现给指定的HtmlTextWriter对象hw
19dg.RenderControl(hw);
20//将字符串写入 HTTP 输出内容流
21Response.Write(sw.ToString());
22//将当前所有缓冲的输出发送到客户端,停止该页的执行,并引发 Application_EndRequest 事件
23Response.End();
24#endregion [导出]