报告处理过程中发生错误。-在ASP.NET MVC中进行RLDC报告

3
我有一个生成报告的操作:
  public ActionResult Report(string id)
        {
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Report"), "Person.rdlc");
            if (System.IO.File.Exists(path))
            {
                lr.ReportPath = path;
            }
            else
            {
                return View("Index");
            }
            List<Person> cm = new List<Person>();

            var viewModel = new PersonIndexData();

            viewModel.People= db.Person
            .Include(k => k.Groups)
            .OrderBy(k => k.Name);

            cm = viewModel.People.ToList();

            ReportDataSource rd = new ReportDataSource("PersonDataSet", cm);
            lr.DataSources.Add(rd);
            string reportType = id;
            string mimeType;
            string encoding;
            string fileNameExtension;

            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;

            renderedBytes = lr.Render(
                reportType,
                null,
                out mimeType,
                out encoding,
                out fileNameExtension,
                out streams,
                out warnings);


            return File(renderedBytes, mimeType);
        }

当我像这样调用此操作:(mysite/person/report/pdf)时,我会得到这个异常:
报告处理期间发生错误。指示此行:
        renderedBytes = lr.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);

您能告诉我为什么在这段代码中会出现异常吗?它没有报任何错误,而且异常也不是很容易理解。我正在使用EF-Code First。谢谢。


看一下警告输出参数,它可能会给你一些提示。 - Andy Baker
@AndyBaker 谢谢。我不确定我是否完全理解了您的意思,但当前在错误列表中没有警告。 - jason
澄清一下。lr.Render方法将填充标记为“out”的参数的值。在执行渲染后,向警告变量添加监视器并查看其包含的内容。 - Andy Baker
@AndyWiesendanger,异常情况是:报告处理过程中发生了错误。谢谢。 - jason
顺便提醒一下,检查一下你的参数(如果有的话)和数据集名称,它们是否正确。 - zchpit
显示剩余4条评论
3个回答

2
在添加了TableAdapter之后,您是否尝试过像这样操作?对我来说它完美地运作。
public FileResult Report(string id)
{
    PersonTableAdapter ta = new PersonTableAdapter();
    PersonDataSet ds = new PersonDataSet();

    //for avoiding "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints." error
    ds.Person.Clear();
    ds.EnforceConstraints = false;

    ta.Fill(ds.Person, id); //You might customize your data at this step i.e. applying a filter

    ReportDataSource rds = new ReportDataSource();
    rds.Name = "ReportingDataSet";
    rds.Value = ds.Person;

    ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
    rv.ProcessingMode = ProcessingMode.Local;
    rv.LocalReport.ReportPath = Server.MapPath("~/Report/Person.rdlc");

    // Add the new report datasource to the report.
    rv.LocalReport.DataSources.Add(rds);
    rv.LocalReport.EnableHyperlinks = true;
    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, "Person" + "_" + id + ".pdf");
}

2
我不确定哪个会帮到你,所以我列出了以下几点:
  1. 尝试从报表菜单中移除数据源,然后再添加它。
  2. 在执行操作之前,请检查Render函数的要求,因此您可能需要检查inout参数值。
  3. 尝试查看LocalReporter周围的一些示例,以更加了解如何使用此工具。

最后,抛出异常的行与主代码不同,因为您将null放在了deviceInfo的位置 致意。


1

我的rdlc解决方案基于ReportViewer。

        byte[] bytes = null;
        string attachmentName = string.Empty;

        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string extension;
        /*GetReportDataSources(logicResult, spec);*/
        var reportPath = GetReportExecutionPath();

        ReportViewer viewer = new ReportViewer();
        /*viewer.LocalReport.SubreportProcessing +=
            new Microsoft.Reporting.WinForms.SubreportProcessingEventHandler(LocalReport_SubreportProcessing);*/
        viewer.LocalReport.ReportPath = reportPath;
        /*viewer.LocalReport.SetParameters(parameters);*/
        viewer.LocalReport.EnableExternalImages = true;
        viewer.RefreshReport();

        viewer.LocalReport.DisplayName = "displayName";
        bytes = viewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension,
            out streamids, out warnings);

/*一些逻辑*/

        return new StreamResponse(() =>
        {
            var pdfOutput = new MemoryStream(bytes); 
            pdfOutput.Seek(0, SeekOrigin.Begin);
            return pdfOutput;
        }, "application/pdf").WithHeader("Content-Disposition", "attachment; filename=" + attachmentName);

你还需要添加报告路径和数据源(我的解决方案比较复杂,我使用LocalReport_SubreportProcessing实现)。
附注:在ASP.MVC中使用rdlc有一个问题。你需要在ISS应用程序池上设置“身份=LocalSystem”,这在Intranet中是可以的,但在Internet中不可行。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接