Asp.net mvc 3 如何在MVC3中使用报表查看器生成报表?

Asp.net mvc 3 如何在MVC3中使用报表查看器生成报表?,asp.net-mvc-3,reportviewer,Asp.net Mvc 3,Reportviewer,我已经在asp.net中使用报表查看器完成了报告。现在我也想在MVC3中创建它。由于我是MVC的新手,期待你们的指导。谢谢 您需要在运行时单独填充数据集,并将其与报表关联 如果您最初使用报表向导创建报表,它应该已经创建了一个您可以使用的数据集定义—在您的示例中是StudentDataSource.xsd。如果打开该文件,您将看到查询以及查询的TableAdapter 下面是一个基于上面提到的问题Kevin的例子() 一个StudentReport.rdlc报表,带有默认的DataSet1数据集和

我已经在asp.net中使用报表查看器完成了报告。现在我也想在MVC3中创建它。由于我是MVC的新手,期待你们的指导。谢谢

您需要在运行时单独填充数据集,并将其与报表关联

如果您最初使用报表向导创建报表,它应该已经创建了一个您可以使用的数据集定义—在您的示例中是StudentDataSource.xsd。如果打开该文件,您将看到查询以及查询的TableAdapter

下面是一个基于上面提到的问题Kevin的例子()

一个StudentReport.rdlc报表,带有默认的DataSet1数据集和生成的StudentDataSet.xsd

以下是在PDFController中修改的File()操作方法:

   public FileResult File() {
        // Create a new dataset
        StudentDataSet ds = new StudentDataSet();

        // Create and fill the Student data table
        // using the Student table adapter

        StudentDataSetTableAdapters.StudentTableAdapter dta =
               new StudentDataSetTableAdapters.StudentTableAdapter();
        dta.Fill(ds.Student);

        // Create a new report datasource with 
        //      Name = the dataset name in the report,
        //      Value = the populated data table.

        ReportDataSource rds = new ReportDataSource();
        rds.Name = "DataSet1";
        rds.Value = ds.Student;

        ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
        rv.ProcessingMode = ProcessingMode.Local;
        rv.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");

        // Add the new report datasource to the report.
        rv.LocalReport.DataSources.Add(rds);

        rv.LocalReport.Refresh();

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

        streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

        return File(streamBytes, mimeType, "StudentReport.pdf");
    }
另外,请注意,如果在上一个问题的ASPXView.aspx页面中使用相同的代码,则需要为MVC项目和正在使用的数据集表适配器导入名称空间

ASPXView.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
           Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<%@ Import Namespace="ProjectNamespace" %>
<%@ Import Namespace="ProjectNamespace.StudentDataSetTableAdapters" %>

<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
    <title>ASPXView</title>
</head>
<body>
    <div>
        <script runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
              StudentDataSet ds = new StudentDataSet();

              StudentTableAdapter dta = new StudentTableAdapter();
              dta.Fill(ds.Student);

              ReportDataSource rds = new ReportDataSource();
              rds.Name = "DataSet1";
              rds.Value = ds.Student;
              ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");
              ReportViewer1.LocalReport.DataSources.Add(rds);
              ReportViewer1.LocalReport.Refresh();
            }
        </script>
        <form id="Form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">          
        </asp:ScriptManager>
        <rsweb:reportviewer id="ReportViewer1" runat="server" height="500" width="500" AsyncRendering="false"></rsweb:reportviewer>
        </form>        
    </div>
</body>
</html>

ASPXView
私有无效页面加载(对象发送方,System.EventArgs e)
{
StudentDataSet ds=新建StudentDataSet();
StudentTableAdapter dta=新的StudentTableAdapter();
dta.Fill(ds.Student);
ReportDataSource rds=新的ReportDataSource();
rds.Name=“DataSet1”;
rds.Value=ds.Student;
ReportViewer1.LocalReport.ReportPath=Server.MapPath(“~/Reports/StudentReport.rdlc”);
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
}

您需要在运行时单独填充数据集,并将其与报表关联

如果您最初使用报表向导创建报表,它应该已经创建了一个您可以使用的数据集定义—在您的示例中是StudentDataSource.xsd。如果打开该文件,您将看到查询以及查询的TableAdapter

下面是一个基于上面提到的问题Kevin的例子()

一个StudentReport.rdlc报表,带有默认的DataSet1数据集和生成的StudentDataSet.xsd

以下是在PDFController中修改的File()操作方法:

   public FileResult File() {
        // Create a new dataset
        StudentDataSet ds = new StudentDataSet();

        // Create and fill the Student data table
        // using the Student table adapter

        StudentDataSetTableAdapters.StudentTableAdapter dta =
               new StudentDataSetTableAdapters.StudentTableAdapter();
        dta.Fill(ds.Student);

        // Create a new report datasource with 
        //      Name = the dataset name in the report,
        //      Value = the populated data table.

        ReportDataSource rds = new ReportDataSource();
        rds.Name = "DataSet1";
        rds.Value = ds.Student;

        ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
        rv.ProcessingMode = ProcessingMode.Local;
        rv.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");

        // Add the new report datasource to the report.
        rv.LocalReport.DataSources.Add(rds);

        rv.LocalReport.Refresh();

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

        streamBytes = rv.LocalReport.Render("PDF", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

        return File(streamBytes, mimeType, "StudentReport.pdf");
    }
另外,请注意,如果在上一个问题的ASPXView.aspx页面中使用相同的代码,则需要为MVC项目和正在使用的数据集表适配器导入名称空间

ASPXView.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0,
           Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
           Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %>
<%@ Import Namespace="ProjectNamespace" %>
<%@ Import Namespace="ProjectNamespace.StudentDataSetTableAdapters" %>

<!DOCTYPE html>

<html>
<head id="Head1" runat="server">
    <title>ASPXView</title>
</head>
<body>
    <div>
        <script runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
              StudentDataSet ds = new StudentDataSet();

              StudentTableAdapter dta = new StudentTableAdapter();
              dta.Fill(ds.Student);

              ReportDataSource rds = new ReportDataSource();
              rds.Name = "DataSet1";
              rds.Value = ds.Student;
              ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/StudentReport.rdlc");
              ReportViewer1.LocalReport.DataSources.Add(rds);
              ReportViewer1.LocalReport.Refresh();
            }
        </script>
        <form id="Form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">          
        </asp:ScriptManager>
        <rsweb:reportviewer id="ReportViewer1" runat="server" height="500" width="500" AsyncRendering="false"></rsweb:reportviewer>
        </form>        
    </div>
</body>
</html>

ASPXView
私有无效页面加载(对象发送方,System.EventArgs e)
{
StudentDataSet ds=新建StudentDataSet();
StudentTableAdapter dta=新的StudentTableAdapter();
dta.Fill(ds.Student);
ReportDataSource rds=新的ReportDataSource();
rds.Name=“DataSet1”;
rds.Value=ds.Student;
ReportViewer1.LocalReport.ReportPath=Server.MapPath(“~/Reports/StudentReport.rdlc”);
ReportViewer1.LocalReport.DataSources.Add(rds);
ReportViewer1.LocalReport.Refresh();
}

我相信这是这个[问题][1]的重复。[1] :谢谢凯文。。。我尝试了这一个,但仍然面临问题…在报告处理过程中发生了错误。studentdataset(我的数据集)请帮助我提供更多关于您尝试过的内容和您遇到的错误的详细信息。我相信这是这个[问题][1]的重复。[1] :谢谢凯文。。。我尝试了这一个,但仍然面临问题…在报告处理过程中发生了错误。studentdataset(我的数据集)请帮助我提供更多有关您尝试过的内容和遇到的错误的详细信息。