【问题标题】:Combine Crystal Reports and WebAPI, is it possible?结合水晶报表和WebAPI,有可能吗?
【发布时间】:2018-01-15 09:50:45
【问题描述】:

我有一个 .NET WebAPI 项目,我已经向它添加了一个 rpt 报告文件。如何添加 Crystal Report WebViewer 组件?

到目前为止我想出了什么:

public class ReportController: ApiController
{
    [HttpGet]
    public CrystalReportViewer GetReport()
    {
        CrystalReportViewer reportViewer = new CrystalReportViewer();
        ReportDocument reportDocument = new ReportDocument();
        reportDocument.Load(HttpContext.Current.Server.MapPath("CrystalReport1.rpt"));
        reportViewer.ReportSource = reportDocument;
        return reportViewer;
    }
}

我在编译过程中得到的错误是

找不到类型或命名空间名称“CrystalReportViewer”。

虽然我的参考列表中有:

  • CrystalDecisions.CrystalReports.Engine
  • CrystalDecisions.ReportSource
  • CrystalDecisions.Shared

但是,我不确定报告如何通过 API。有更多知识的人可以对此有所了解吗?

【问题讨论】:

  • 您所要求的内容是零意义的。你到底希望发生什么?如果你想要一个 html 页面,我认为它仍然只支持 WebForms,如果你想要一个 pdf,你也可以使用 that
  • @FilipCordas 所以我想我应该问一个问题“如何在现有的 WebAPI 应用程序中重新实现现有的 VB6 WinForms CrystalReport 应用程序”。
  • 可能,但我认为关于这个主题有很多东西。

标签: c# asp.net-web-api crystal-reports


【解决方案1】:

我现在将 PDF 或 XLSX 创建为流,将其复制到内存流中,并将该内容作为 base64 编码字符串发送回。然后将 base64 作为数据 URL 加载到 iframe 中。

后端:

[HttpGet]
public AjaxAnswer<string> GetReport(...)
{
    ReportDocument reportDocument = new ReportDocument();
    ...
    Stream s = reportDocument.ExportToStream(ExportFormat.FileFormat);
    MemoryStream ms = new MemoryStream();
    s.CopyTo(ms);
    return new AjaxAnswer() {
        success = true,
        data = Convert.ToBase64String(ms.ToArray()
    };
}

前端:

form.submit({
    ...
    success: function(form, operation) {
        pnl.setLoading(false);
        var pdfBase64 = Ext.decode(operation.response.responseText, true).data;
        if(pdfBase64) {
            if(!Ext.isIE && !Ext.isEdge && !btn.isXLS) {
                document.querySelector(".iframe-report-pdf").src = "data:application/pdf;base64," + pdfBase64;
            } else {
                var type = (btn.isXLS?'application/xlsx':'application/pdf');
                var filename = (btn.report || btn.text)+(btn.isXLS?'.xls':'.pdf');

                Ext.Download(Ext.B64ToBlob(pdfBase64, type), filename, type);
            }
        }
    },

【讨论】:

  • 离题,但这个解决方案的扩展性如何?对于旧的 CR 版本,我们做了类似的事情,它给 web 服务器带来了令人讨厌的压力;对于我们的新 API 设计,我们刚刚排除了 CR 支持(到目前为止)。
  • @Arvo 它的扩展性不是很好,但是 PDF 一次只能由一个人创建(然后打印),通常每月一次或两次。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 2011-04-30
  • 1970-01-01
相关资源
最近更新 更多