【发布时间】:2011-08-23 05:43:04
【问题描述】:
需要在我的 MVC3 应用程序中生成报告,如何在 mvc3 中使用 RDLC。请任何人都可以给我一个示例性解释和指导,以便在我的 MVC3 应用程序中创建 RDLC 报告。
谢谢
【问题讨论】:
-
可以是RDL,还是必须是rdlc?
标签: c# asp.net asp.net-mvc-3
需要在我的 MVC3 应用程序中生成报告,如何在 mvc3 中使用 RDLC。请任何人都可以给我一个示例性解释和指导,以便在我的 MVC3 应用程序中创建 RDLC 报告。
谢谢
【问题讨论】:
标签: c# asp.net asp.net-mvc-3
我最近在 MVC3 应用程序中使用 RDLC 报告将结果导出到 Excel 电子表格中。
private class ExcelReport
{
private string encoding;
private string[] streams;
private Warning[] warnings;
private string fileNameExtension;
private string mimeType;
public ExcelReport()
{
this.ReportDataSources = new List<ReportDataSource>();
}
public string ExportFileName { get; set; }
public string MimeType
{
get
{
return mimeType;
}
private set
{
mimeType = value;
}
}
public string Encoding
{
get
{
return encoding;
}
private set
{
encoding = value;
}
}
public string FileNameExtension
{
get
{
return fileNameExtension;
}
private set
{
fileNameExtension = value;
}
}
public string[] Streams
{
get
{
return streams;
}
private set
{
streams = value;
}
}
public Warning[] Warnings
{
get
{
return warnings;
}
private set
{
warnings = value;
}
}
public string ReportPath { get; set; }
public IList<ReportDataSource> ReportDataSources { get; set; }
public byte[] GetReport()
{
LocalReport localReport = new LocalReport();
localReport.ReportPath = this.ReportPath;
foreach (var source in this.ReportDataSources)
{
localReport.DataSources.Add(source);
}
string reportType = "Excel";
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>Excel</OutputFormat>" +
" <PageWidth>21cm</PageWidth>" +
" <PageHeight>29cm</PageHeight>" +
" <MarginTop>1cm</MarginTop>" +
" <MarginLeft>2cm</MarginLeft>" +
" <MarginRight>2cm</MarginRight>" +
" <MarginBottom>1cm</MarginBottom>" +
"</DeviceInfo>";
//Render the report
return localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings
);
}
}
控制器
public ActionResult GetReport(string reportParameter1)
{
/*Get data for your report that matches the dataset format in the report.*/
IEnumerable<ReportData> list = new IEnumerable<ReportData> /*Your Report Data*/
{
new ReportData{Id = 1, Code="ABC"},
new ReportData{Id = 2, Code="DEF"}
};
var excelReport = new ExcelReport
{
ExportFileName = "Your File Name",
ReportPath = Server.MapPath("~/Content/Reports/YourReport.rdlc")
};
var ds = new ReportDataSource("Main", list); /* Main is the name of the dataset inside the report*/
excelReport.ReportDataSources.Add(ds);
var report = excelReport.GetReport();
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.{1}", excelReport.ExportFileName, excelReport.FileNameExtension));
Response.ContentType = "application/vnd.ms-excel";
return File(report, excelReport.MimeType);
}
最终结果应该是您将报告导出为 excel 文档。
【讨论】:
这不是一个简单的任务!这里有六个步骤供您完成。
1. Web.config
首先,打开您的 web.config 并将以下内容添加到您的 <system.web> 部分
<httpHandlers>
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
validate="false" />
</httpHandlers>
然后在您的<compilation> 标签内,添加以下内容:
<buildProviders>
<add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</buildProviders>
然后在你的<system.webServer> 标签内添加这个:
<handlers>
<add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</handlers>
还添加对 Microsoft.ReportViewer.Common.dll 和 Microsoft.ReportViewer.WebForms.dll(版本 10.0.0.0)的引用。
2。 Global.asax
打开您的 Global.asax.cs 并在 RegisterRoutes() 函数中添加以下内容:
//Reports Viewers depend on Viewstate so are hosted in class ASPX web forms, so bypassing MVC3 routing
routes.IgnoreRoute("ReportViewer/");
执行此操作时,在您的 asp.net 应用程序中创建一个名为“ReportViewer”的新文件夹。
3. XSD 数据集
在项目文件夹的 ROOT 中创建一个新的 XSD 文件(是的,它必须是根目录!)。
右键单击您的项目,单击“添加新数据集”。它应该是空的。
右键单击它并转到添加,TableAdapter...
选择您的数据库连接字符串。将其保存在应用程序配置中。使用现有的存储过程。使用 SELECT 查询填充数据集。给数据集一个有意义的名字。
4.创建您的 RDLC 报告
创建一个新的 RDLC 报告文件(为此您需要 BI 工具),然后转到“添加数据集”。选择您的新数据集。创建报告。记下数据集名称。 (例如,“DataSet1”,记得稍后将其更改为更有用的东西。)
5.设置 ASPX 页面
创建一个新的 ASP.NET WebForm,是的,webform 页面标题为 ShowReport.aspx。 HTML 应具有以下内容:
<body>
<form id="form1" runat="server" >
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<rsweb:ReportViewer
ID="ReportViewer1"
SizeToReportContent="False"
Width="820px"
Height="820px"
runat="server"
ShowRefreshButton="false"
AsyncRendering="false"
DocumentMapCollapsed="True"
PageCountMode="Actual"
PromptAreaCollapsed="True"></rsweb:ReportViewer>
</form>
</body>
6.代码隐藏
在您的 reports.aspx.cs 的代码隐藏中,执行以下操作:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataSet dataset = GetPopulatedDatasetFromDB() // Populate your dataset here by calling the stored proc.
ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Local;
ReportViewer1.LocalReport.ReportPath = Server.MapPath("/Reports/MyNewReport.rdlc");
ReportViewer1.LocalReport.DataSources.Clear();
ReportViewer1.LocalReport.DataSources.Add(
new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", dataset.Tables[0]));
ReportViewer1.LocalReport.Refresh();
}
}
访问您的页面:
http://localhost/MyApp/ReportViewer/ShowReport.aspx
瞧!
【讨论】:
您必须手动生成报告,如上例所示。您可能希望使用 pdf 以便用户可以在浏览器中查看它(如果安装了 acrobat),而不必先下载它。
【讨论】:
请按照以下步骤操作:
【讨论】: