做过一个WEBGIS的项目,要动态出表,并且把统计报表的数据直接生成到页面上。做过两种方法
1.直接把数据生成为一个HTML静态页面,先将数据生成静态Table的字符串
- using System.IO ;
- using System.Xml ;
- private void CreatHtm(string TableStr)
- {
- string mpath = Path.Combine(MapPath("."),"SortArea_HTM//") ;
- string FileName = System.Guid.NewGuid().ToString() + ".htm" ;
- string FileNameUrl = Request.ApplicationPath + "/" + "SortArea_HTM" + "/" + FileName;
- string content = String.Format("<HTML><HEAD></HEAD><BODY>{0}</BODY></HTML>",TableStr) ;
- FileStream fs = new FileStream(mpath + FileName,FileMode.CreateNew,FileAccess.Write,FileShare.None);
- StreamWriter sr = new StreamWriter(fs,System.Text.Encoding.GetEncoding("Gb2312"));
- sr.WriteLine(content);
- sr.Close();
- sr = null;
- fs.Close() ;
- fs = null ;
- LinkUrl.Text = FileNameUrl ;
- string LinkStr = FileNameUrl.Trim() ;
- Response.Write("<script>");
- Response.Write(String.Format("window.open('{0}')",LinkStr)) ;
- Response.Write("</script>");
- }
2.把数据传递到动态页面上,显示出来
- <asp:Table id="ResultTab" align=center cellSpacing="0" borderColorDark="white" cellPadding="4" borderColorLight="silver" border="0" runat="server" Width=1000>
- //添加一组数据到统计表
- TableRow rr = new TableRow();
- TableCell c = new TableCell();
- c.Controls.Add(new LiteralControl(OString));
- rr.Cells.Add(c);
- ResultTab.Rows.Add(rr);
在页面上生成有个好处:可以直接打印成Excel之类,比较方便
- public void ExportToExcel()
- {
- Response.Clear();
- Response.Buffer = true;
- Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");
- Response.ContentEncoding = System.Text.Encoding.UTF8;
- Response.ContentType = "application/vnd.ms-excel";
- this.EnableViewState = false;
- }