我最近使用 Visual Studio 2010 为我的 MVC 应用程序创建了我的第一个 RDLC 报告。
我使用 Report Wizard 创建 Report1.rdlc 并运行以下控制器代码将输出呈现为 PDF。
报表运行时我的数据源引用了我的模型中的站点对象。一切运行良好,但输出中的 4 列中有 2 列呈现为 #Error
两列都有每条记录的数据,没有空值
使 2 #Error 列与呈现的列不同的唯一原因是它们比模型低一级。即4列:
第一个字段 SiteDescription 呈现正常 第二个字段 SiteOperator 将每行的值呈现为 #Error 第三个字段 SiteStatus 将每行的值呈现为 #Error 第四个字段 CapacityMW 呈现正常
注意:SiteOperator 是 Site.SiteOperator.Operator。第三场类似。第一个和第四个是 Site 表中的字段(即本例中的顶层模型)
问题:开发人员还需要做些什么来允许 RDLC 使用模型中的现有对象吗?即我注意到报告向导创建了一个 Report1.rdlc.xml 文件,也许这必须修改,我已经没有这个想法了。任何评论都非常感谢
这是我的控制器代码:
private void RenderReport(string ReportPath, object Model)
{
var localReport = new LocalReport { ReportPath = ReportPath };
var reportDataSource = new ReportDataSource("DataSet1", Model);
localReport.DataSources.Add(reportDataSource);
var reportType = "PDF";
string mimeType;
string encoding;
string fileNameExtension;
//The DeviceInfo settings should be changed based on the reportType
//http://msdn2.microsoft.com/en-us/library/ms155397.aspx
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</OutputFormat>" +
" <PageWidth>8.5in</PageWidth>" +
" <PageHeight>11in</PageHeight>" +
" <MarginTop>0.5in</MarginTop>" +
" <MarginLeft>1in</MarginLeft>" +
" <MarginRight>1in</MarginRight>" +
" <MarginBottom>0.5in</MarginBottom>" +
"</DeviceInfo>";
Warning[] warnings;
string[] streams;
byte[] renderedBytes;
//Render the report
renderedBytes = localReport.Render(
reportType,
deviceInfo,
out mimeType,
out encoding,
out fileNameExtension,
out streams,
out warnings);
//Clear the response stream and write the bytes to the outputstream
//Set content-disposition to "attachment" so that user is prompted to take an action
//on the file (open or save)
Response.Clear();
Response.ContentType = mimeType;
Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);
Response.BinaryWrite(renderedBytes);
Response.End();
}