结合 Crystal 报表和 WebAPI,可能吗?
Combine Crystal Reports and WebAPI, is it possible?
我有一个 .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;
}
}
我在编译过程中得到的错误是
The type or namespace name "CrystalReportViewer" could not be found.
尽管我的参考列表中有:
- CrystalDecisions.CrystalReports.Engine
- CrystalDecisions.ReportSource
- CrystalDecisions.Shared
但是,我不确定报告如何通过API。有更多知识的人可以解释一下吗?
我现在正在创建 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);
}
}
},
我有一个 .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;
}
}
我在编译过程中得到的错误是
The type or namespace name "CrystalReportViewer" could not be found.
尽管我的参考列表中有:
- CrystalDecisions.CrystalReports.Engine
- CrystalDecisions.ReportSource
- CrystalDecisions.Shared
但是,我不确定报告如何通过API。有更多知识的人可以解释一下吗?
我现在正在创建 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);
}
}
},