将报告绑定到web mvc2中的reportviewer

时间:2011-03-14 08:53:56

标签: visual-studio-2008 asp.net-mvc-2 reportviewer

我有asp.net MVC2应用程序。 我正在使用VS2008,并希望将生成的报告从我的控制器连接到reportviewer。

任何想法?

到目前为止我有这个代码 “控制器”

//should pass data to report
 public ActionResult GenerateReport()
      {


        LocalReport report = new LocalReport();
        report.ReportPath = Server.MapPath("~/Reports/KingsCourt.rdlc");

        List<InvoiceRow> rows = new List<InvoiceRow>();

        rows.Add(new InvoiceRow { name = "Testing item", value = (decimal)25.85 });
        rows.Add(new InvoiceRow { name = "Testing item2", value = (decimal)5.15 });
        ReportDataSource source = new ReportDataSource("InvoiceRow", rows);
        report.DataSources.Add(source);

        ViewData["InvoiceRow"] = report;
        return View();
      }

和查看页面:

 <form id="form1" runat="server">

  <h2>GenerateReport</h2>
  <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
      Font-Size="8pt" Height="400px" Width="400px">
    <LocalReport ReportPath="Reports\KingsCourt.rdlc">
      <DataSources>
        <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="InvoiceRow" />
      </DataSources>
    </LocalReport>


  </rsweb:ReportViewer>    

  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Rows" 
      TypeName="Accounts.Classes.Invoices"></asp:ObjectDataSource>
 </form>

3 个答案:

答案 0 :(得分:9)

您始终可以将ASP.NET WebForms与MVC一起使用。我想这是唯一的办法 我为你准备了一个样本here

您可以创建一个文件夹,放置您的asp.net WebForm和报告(rdlc)。 我把架构(xsd)和数据(xml)放在同一个文件夹中,但显然,我猜你要使用数据库。 我已经将路线映射到这样的报告:

    //Custom route for reports
    routes.MapRoute(
     "ReportRoute",
     "Reports/{reportname}",                
     "~/Reports/{reportname}.aspx"
     );

<强>更新

我为ASP.NET MVC3(MvcReportViewerMVC3)准备了一些code
它与webform上的一些小改动几乎相同:我已经集成了新的ReportViewer(10),我不得不将ScriptManager添加到同一页面:

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

我已经更改了WebForm代码,因为看起来page_load事件被称为加载次数:

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
    ....
    }
}

希望它有所帮助。

答案 1 :(得分:0)

可以在asp.net mvc中使用reportviewer

I doubt 您可以做的最好的事情是返回报告rendered as an image

答案 2 :(得分:0)

帕维尔,我已经挣扎了一会儿。最后决定实施我自己的 ActionResult 并流式传输PDF文件。 这是我的行动:

public ActionResults.DownloadFileResult PrintReport()
{
    rvDocument = new WebForms.ReportViewer();

    rvDocument.ProcessingMode = ProcessingMode.Local;

    LocalReport report = rvDocument.LocalReport;
    report.ReportPath = Server.MapPath("~/Reports/KingsCourt.rdlc");

    string sGuid = Guid.NewGuid.ToString.Replace("-", "");

    List<InvoiceRow> rows = new List<InvoiceRow>();

    rows.Add(new InvoiceRow { name = "Testing item", value = (decimal)25.85 });
    rows.Add(new InvoiceRow { name = "Testing item2", value = (decimal)5.15 });
    ReportDataSource source = new ReportDataSource("InvoiceRow", rows);
    report.DataSources.Add(source);

    rvDocument.LocalReport.Refresh();

    string sFileName = Path.Combine("<temp folder>", sGuid + ".PDF");
    byte[] StreamBytes = null;

    string mimeType = "";
    string encoding = "";
    string filenameExtension = "";
    string[] streamids = null;
    Warning[] warnings = null;

    StreamBytes = rvDocument.LocalReport.Render("PDF", null, mimeType, encoding, filenameExtension, streamids, warnings);
    if ((StreamBytes != null)) {
        try {
            using (FileStream fs = new FileStream(sFileName, FileMode.CreateNew, FileAccess.Write, FileShare.Write)) {
                fs.Write(StreamBytes, 0, StreamBytes.Length);
            }
        } catch (Exception ex) {
            // Log.Logger.WriteException(string.Empty, ex, Log.Logger.LogTypes.Error, this.GetType, "Report001_Load");
        }
        StreamBytes = null;
    }

    BPMVC.ActionResults.DownloadFileResult oPdfToStream = new BPMVC.ActionResults.DownloadFileResult();
    {
        oPdfToStream.FileName = sFileName;
        oPdfToStream.ContentType = "application/pdf";
        oPdfToStream.DocumentName = "ReportName.pdf";
    }

    return (oPdfToStream);

}

这是我的自定义ActionResult

using System.Web.Mvc;
using System.Web;
using System.IO;

namespace ActionResults
{

    public class DownloadFileResult : ActionResult
    {

        #region " Public Properties "

        private string _VirtualPath = "";
        public string VirtualPath {
            get { return (_VirtualPath); }
            set { _VirtualPath = value; }
        }

        private string _FileName = "";
        public string FileName {
            get { return (_FileName); }
            set { _FileName = value; }
        }

        private string _ContentType = "text/html";
        public string ContentType {
            get { return (_ContentType); }
            set { _ContentType = value; }
        }

        private string _DocumentName = "";
        public string DocumentName {
            get { return (_DocumentName); }
            set { _DocumentName = value; }
        }

        #endregion


        public override void ExecuteResult(System.Web.Mvc.ControllerContext context)
        {
            if ((context == null)) {
                throw new ArgumentNullException("context");
            }

            if (string.IsNullOrEmpty(this.FileName) && string.IsNullOrEmpty(this.VirtualPath)) {
                throw new ArgumentNullException("FileName is Empty");
            }


            string filePath = "";

            if (string.IsNullOrEmpty(this.VirtualPath)) {
                filePath = this.FileName;
            } else {
                filePath = context.HttpContext.Server.MapPath(this.VirtualPath);
            }

            if (string.IsNullOrEmpty(this.DocumentName)) {
                this.DocumentName = Path.GetFileName(filePath);
            }

            {
                context.HttpContext.Response.Clear();
                context.HttpContext.Response.ClearHeaders();
                context.HttpContext.Response.ClearContent();
                context.HttpContext.Response.ContentType = this.ContentType;
                context.HttpContext.Response.AddHeader("Content-Type", this.ContentType);
                context.HttpContext.Response.AddHeader("content-disposition", "attachment; filename=\"" + this.DocumentName + "\"");
                context.HttpContext.Response.TransmitFile(this.FileName);
                context.HttpContext.Response.End();
            }

        }

    }

}