码迷,mamicode.com
首页 > Web开发 > 详细

MVC引用asp.net报表(测试小例子)

时间:2018-04-10 13:40:10      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:有一个   report   token   isp   前端   led   repo   tty   分享图片   

技术分享图片
 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             List<string> fileName = 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     }
MVC后端
技术分享图片
 1 @{
 2     ViewBag.Title = "Index";
 3 }
 4 <script src="~/Scripts/jquery-1.8.2.min.js"></script>
 5 <span>站点:<input type="text" id="siteid"></span>
 6 <select id="SelReport"></select>
 7 
 8 
 9 <input type="button" value="报表" onclick="getReport()" />
10 <br />
11 <iframe id="IframeUrl" name="IframeUrl" style="width: 100%; height: 1200px;"></iframe>
12 
13 <script type="text/javascript">
14     function getReport() {
15         var sel = $("#SelReport").val();
16         var site = $("#siteid").val();
17         $.ajax({
18             url: "/Default1/ExistsFile",
19             data: { "keyId": sel },
20             type: "post",
21             dataType: "json",
22             success: function (data) {
23                 if (data.ResultBool) {
24                     $("#IframeUrl").attr("src", "../WebForm1.aspx?KeyId=" + sel + "&site=" + site);
25                 } else
26                     alert(sel + "报表不存在!");
27             }
28 
29         });
30     }
31 
32     $(document).ready(function () {
33         $.ajax(
34         {
35             url: /Default1/ReportFileName,
36             data: ‘‘,
37             type: post,
38             dataType: json,
39             success: function (data) {
40                 if (data != null) {
41                     var option;
42                     $.each(data.Result, function (i, v) {
43                         option += "<option>" + v + "</option>";
44                     });
45                     $("#SelReport").html(option);
46                 }
47             }
48         });
49     });
50 </script>
MVC前端
技术分享图片
  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", "<script>Myfun();</script>");
 55             //Response.Write("<script type=‘text/javascript‘>setTimeout(‘Myfun()‘,‘1000‘);function Myfun(){ $(‘#ReportViewer1_ctl05_ctl04_ctl00_Menu>div>a:eq(2)‘).click();}</script>");
 56             //string js =
 57             //    "<script src=\"Scripts/jquery-1.8.2.min.js\"></script><script type=‘text/javascript‘> " +
 58             //    "var t1 = window.setTimeout(Myfun, 100); " +
 59             //    "function Myfun() {  " +
 60             //    "try {  " +
 61             //    "if ($(‘#ReportViewer1_ctl05_ctl04_ctl00_Menu>div>a:eq(2)‘).length > 0) " +
 62             //    "{ console.log(‘test‘);" +
 63             //    "$(‘#ReportViewer1_ctl05_ctl04_ctl00_Menu>div>a:eq(2)‘).click();" +
 64             //    " window.clearTimeout(t1);} " +
 65             //    "} " +
 66             //    "catch (err) " +
 67             //    "{}" +
 68             //    "}</script>";
 69             //Response.Write(js);
 70         }
 71 
 72 
 73 
 74         /// <summary>
 75         /// 执行查询语句,返回DataTable
 76         /// </summary>
 77         /// <param name="sqlString">查询语句</param>
 78         /// <returns>DataTable</returns>
 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     }
WebForm后端
技术分享图片
 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 <script src="Scripts/jquery-1.8.2.min.js"></script>
 5 <script src="Scripts/jquery-ui-1.8.24.js"></script>
 6 <!DOCTYPE html>
 7 <html>
 8 <head runat="server">
 9     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
10     <title></title>
11     <style>
12         body {
13             margin: 0;
14             padding: 0;
15         }
16     </style>
17     <%--    <script type="text/javascript">
18         //function Myfun() {
19         //    //console.log("test");
20         //    ////$("#ReportViewer1_ctl05_ctl04_ctl00_Menu>div").setAttribute(‘style‘, ‘border: 1px solid transparent, background-color: transparent, cursor: default‘);
21         //    ////$("#ReportViewer1_ctl05_ctl04_ctl00_Button").click();
22 
23         //    //$("#ReportViewer1_ctl05_ctl04_ctl00_Menu").removeAttr("style");
24         //    //$("#ReportViewer1_ctl05_ctl04_ctl00_Menu>div>a").eq(2).click();
25         //    return true;
26         //}
27     </script>--%>
28 </head>
29 <body>
30     <form id="form1" runat="server">
31         <table align="center" border="0" cellpadding="0" cellspacing="0">
32             <tr>
33                 <td>
34                     <asp:ScriptManager ID="ScriptManager1" runat="server">
35                     </asp:ScriptManager>
36                     <rsweb:ReportViewer ID="ReportViewer1" runat="server" Height="100%" Width="98%" Font-Names="Verdana" Font-Size="8pt" SizeToReportContent="true">
37                     </rsweb:ReportViewer>
38                 </td>
39             </tr>
40         </table>
41     </form>
42 </body>
43 </html>
WebForm前端

还有一个文件夹Report下面有.rdl报表。这里就不写了。

MVC引用asp.net报表(测试小例子)

标签:有一个   report   token   isp   前端   led   repo   tty   分享图片   

原文地址:https://www.cnblogs.com/Zhengxue/p/8777944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!