在我的MVC3剃须刀应用程序中,我使用以下代码进行报告
位指示
ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
rv.ProcessingMode = ProcessingMode.Local;
rv.LocalReport.ReportPath = Server.MapPath("~/Reports/TestReport.rdlc");
rv.LocalReport.Refresh();
byte[] streamBytes = null;
string mimeType = "";
string encoding = "";
string filenameExtension = "";
string[] streamids = null;
Warning[] warnings = null;
streamBytes = rv.LocalReport.Render("RDLC", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);
return File(streamBytes, mimeType, "TestReport.rdlc");
ASPX视图
<div>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/TestReport.rdlc");
ReportViewer1.LocalReport.Refresh();
}
</script>
<form id="Form1" runat="server" method="get" action="/Pag1/File">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:reportviewer id="ReportViewer1" runat="server" height="500" width="500" AsyncRendering="false"></rsweb:reportviewer>
</form>
</div>
这里我得到了结果,因为PDF需要用pdfviewer打开它。我只想display the report in viewr
。我是MVC3的新手。
如果有任何机构知道请分享
上述代码的参考是here
答案 0 :(得分:1)
以下是我在其中一个网站中使用的ActionMethod生成报告的示例:
public ActionResult WeeklyAisleReport(DateTime start, DateTime end)
{
var range = new DateRange(start, end);
var records = _repository.Select(range, "");
var formattedRecords = AisleProductivityRecord.Generate(records).ToList();
var localReport = new LocalReport
{
ReportPath =
Server.MapPath("~/Content/Reports/PTLWeeklyProductivity.rdlc")
};
var pickRecords = new ReportDataSource("PickRecords",formattedRecords);
localReport.DataSources.Add(pickRecords);
const string ReportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
Warning[] warnings;
string[] streams;
//Render the report
byte[] renderedBytes = localReport.Render(
ReportType,
null, //deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
Response.AddHeader("content-disposition",
"attachment; filename=WeeklyAisleReport-" + start.ToString("yyyy_MM_dd") + "." +
fileNameExtension);
return File(renderedBytes, mimeType);
}
关于View,您不能在MVC应用程序中使用WebForms标记(,等等)。您需要创建一个发布到生成PDF的ActionMethod的表单。
你的剃刀视图文件看起来应该是这样的(以我的方法为例):
@using (Html.BeginForm("WeeklyAisleReport", "PTL"))
{
@Html.TextBox("start")
@Html.TextBox("end")
<input type="submit" value="View Report"/>
}
答案 1 :(得分:1)
从工具箱中为您的ASPX页面添加报表查看器和脚本管理器。
SizeToReportContent = "true"
不要忘记将以上属性添加到报告查看器
然后您的页面如下所示
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportView.aspx.cs" Inherits="ERP.Reports.ReportView" %>
<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="true" ShowPrintButton="true" Height="649px" Width="1105px" SizeToReportContent = "true">
</rsweb:ReportViewer>
</div>
<script>
</script>
</form>
</body>
</html>
在ASPX代码页中添加此代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
RenderReportModels();
}
}
private void RenderReportModels()
{
List<Company> comp = new List<Company>();
CompanyBAL bal = new CompanyBAL();
comp = bal.SampleData();//Load Data
// Clear out any previous data sources.
this.ReportViewer1.LocalReport.DataSources.Clear();
// Set report mode for local processing.
ReportViewer1.ProcessingMode = ProcessingMode.Local;
// Validate report source.
var rptPath = Server.MapPath(@"./rdlc/MyReport.rdlc");
//@"E:\ERP\Reports\rdlc\MyReport.rdlc";
if (!File.Exists(rptPath))
return;
// Set report path.
this.ReportViewer1.LocalReport.ReportPath = rptPath;
// Load the dataSource.
var ds1 = ReportViewer1.LocalReport.GetDataSourceNames();
ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource(ds1[0], comp));
// Refresh the ReportViewer.
ReportViewer1.LocalReport.Refresh();
}
添加控制器和相应的视图(cshtml)将以下代码添加到视图页面
<div>
<input type="submit" onclick="return ReportValidationCheck();" name="ShowReport"
value="Show Report" />
</div>
<iframe id="ifrmReportViewer" frameborder="0" width="1000" height="800" style="overflow:hidden;" scrolling="no"></iframe>
<script>
function ReportValidationCheck() {
var url = "../Reports/ReportView.aspx"; //your ASPX page path
var myframe = document.getElementById("ifrmReportViewer");
if (myframe !== null) {
if (myframe.src) {
myframe.src = url;
}
else if (myframe.contentWindow !== null && myframe.contentWindow.location !== null) {
myframe.contentWindow.location = url;
}
else { myframe.setAttribute('src', url); }
}
return false;
}
</script>