4

生成报告时遇到问题。

我应该使用数据库中的所有数据为其动态生成报告(源名称和存储为 xml 字符串的 rdlc 报告)

所以我解决了下一个问题:

  1. 使用报告 xml 字符串创建本地 rdlc 文件;
  2. 设置文件路径;

    var rViewer = new ReportViewer();
    rViewer.LocalReport.ReportPath = @"Path to the generated xml rdlc file";
    var dataSource = new ReportDataSource("DataSourceName", data);
    rViewer.LocalReport.DataSources.Add(dataSource);
    

我可以直接在代码隐藏中指定报告 XML 字符串,而不是生成 rdlc 文件并设置它的路径吗?你能推荐一个更好的方法吗?

谢谢你

4

1 回答 1

5

是的,您可以像这样使用 LoadReportDefinition 为其提供一个 StringReader 对象,

private void Page_Load(object sender, System.EventArgs e)
{
    NMBS.ViewModels.ViewModelReport Report = (Session["CurrentReport"] as NMBS.ViewModels.ViewModelReport);
ReportViewer.ProcessingMode = ProcessingMode.Local;
ReportViewer.PageCountMode = PageCountMode.Actual;

// Set report specifics.
ReportViewer.LocalReport.DisplayName = Report.Name;
ReportViewer.LocalReport.LoadReportDefinition(new System.IO.StringReader(Report.Definition));
foreach (ReportParameter Param in Report.Parameters)
{
    ReportViewer.LocalReport.SetParameters(Param);
}
foreach (ReportDataSource Rds in Report.Data)
{
    //Load Report Data.
    ReportViewer.LocalReport.DataSources.Add(Rds);
}
ReportViewer.CurrentPage = Report.CurrentPage;
ReportViewer.Width = Report.Width;

// Refresh the reports.
ReportViewer.LocalReport.Refresh();
}

其中 Report.Defintion 是包含报告定义的 XML 的字符串。

于 2013-06-12T17:35:58.770 回答