1 public class Default1Controller : Controller 2 { 3 // 4 // GET: /Default1/ 5 6 public ActionResult Index() 7 { 8 return View(); 9 }10 11 public JsonResult ReportFileName()12 {13 DirectoryInfo d = new DirectoryInfo(Server.MapPath("~/") + "Report/");14 FileSystemInfo[] fsinfos = d.GetFileSystemInfos();15 ListfileName = fsinfos.Where(p => p.Name.IndexOf(".rdlc", StringComparison.Ordinal) == -1).Select(fsinfo => fsinfo.Name.Replace(".rdl", "")).ToList();16 return Json(new { Result = fileName });17 }18 19 public JsonResult ExistsFile(string keyId)20 {21 string path = Server.MapPath("~/") + "Report/" + keyId + ".rdl";22 return this.Json(new { ResultBool = System.IO.File.Exists(path) });23 }24 }
1 @{ 2 ViewBag.Title = "Index"; 3 } 4 5 站点: 6 7 8 9 10 11 12 13
1 public partial class WebForm1 : System.Web.UI.Page 2 { 3 protected void Page_Load(object sender, EventArgs e) 4 { 5 if (!IsPostBack) 6 { 7 BindReport(Request["KeyId"]); 8 } 9 } 10 11 12 13 14 private void BindReport(string keyId) 15 { 16 string sql = string.Empty; 17 string site = Request["site"]; 18 switch (keyId) 19 { 20 case "无标题": 21 sql = " select * from sysuser"; 22 if (site.Length > 0) 23 { 24 sql += " where defsite like '" + site.Trim() + "%'"; 25 } 26 break; 27 case "有标题": 28 sql = " select * from facility"; 29 if (site.Length > 0) 30 { 31 sql += " where siteid like '" + site.Trim() + "%'"; 32 } 33 break; 34 case "报表模板": 35 sql = "SELECT USERID AS SEQ,PERSONID AS KSSJ,DEFSITE AS JSSJ FROM SYSUSER"; 36 if (site.Length > 0) 37 { 38 sql += " where siteid like '" + site.Trim() + "%'"; 39 } 40 break; 41 default: 42 break; 43 } 44 45 DataTable dt = new DataTable(); 46 ReportViewer1.ProcessingMode = ProcessingMode.Local; 47 ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/") + @"Report/" + keyId + ".rdl"; //可以通过SQL语句传参形式 48 ReportViewer1.LocalReport.EnableExternalImages = true; 49 dt = QueryTable(sql); 50 ReportDataSource rds = new ReportDataSource("DataSet1", dt); //DataSetl报表数据集名称 51 ReportViewer1.LocalReport.DataSources.Clear(); 52 ReportViewer1.LocalReport.DataSources.Add(rds); 53 ReportViewer1.LocalReport.Refresh(); 54 //ClientScript.RegisterClientScriptBlock(ClientScript.GetType(), "MyScript", ""); 55 //Response.Write(""); 56 //string js = 57 // ""; 69 //Response.Write(js); 70 } 71 72 73 74 ///75 /// 执行查询语句,返回DataTable 76 /// 77 /// 查询语句 78 ///DataTable 79 public static DataTable QueryTable(string sqlString) 80 { 81 82 using (var connection = new OracleConnection("DATA SOURCE=192.168.1.61:5050/Xinsida;USER ID=BKCYUN;PASSWORD=bkc123456;")) 83 { 84 var dt = new DataTable(); 85 try 86 { 87 connection.Open(); 88 var command = new OracleDataAdapter(sqlString, connection); 89 command.Fill(dt); 90 return dt; 91 } 92 catch (Exception e) 93 { 94 throw e; 95 } 96 finally 97 { 98 if (connection.State == ConnectionState.Open) 99 {100 connection.Close();101 }102 }103 }104 }105 protected override void Render(HtmlTextWriter writer)106 {107 using (StringWriter sw = new StringWriter())108 {109 HtmlTextWriter tmpWriter = new HtmlTextWriter(sw);110 base.Render(tmpWriter);111 string val = sw.ToString();112 val = val.Replace(@"!= 'javascript:\'\''", @"!= 'javascript:\'\'' && false");113 writer.Write(val);114 }115 }116 }
1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ReportBuilderTest1.WebForm1" %> 2 3 <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> 4 5 6 7 8 9 1042 4311 17 <%-- --%>28 29 30
还有一个文件夹Report下面有.rdl报表。这里就不写了。