我在asp.net中使用报告查看器进行了报告。现在我想在MVC 3中创建它。因为我对MVC很新,所以期待你们的指导。谢谢!!!
答案 0 :(得分:0)
您需要在运行时单独填充数据集并将其与报告关联。
如果您使用报告向导来初始创建报告,那么它应该创建一个您可以使用的数据集定义 - 在您的情况下,StudentDataSource.xsd。如果您打开该文件,您将看到查询以及查询的TableAdapter。
以下是基于凯文上面提到的问题(How can I use a reportviewer control in an asp.net mvc 3 razor view?)
的示例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>