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

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

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

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

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

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

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

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

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