我有以下代码 它的功能是使用CrystalReports从屏幕上的报表加载数据。
Dim strExportFile As String
strExportFile = "ReportReajustesAplicados.pdf"
Dim s As System.IO.MemoryStream = relat.ExportToStream(ExportFormatType.PortableDocFormat)
With HttpContext.Current.Response
.ClearContent()
.ClearHeaders()
.ContentType = "application/pdf"
.AddHeader("Content-Disposition", "inline; filename=" & strExportFile)
.BinaryWrite(s.ToArray)
.End()
End With
当我提取数据时。
我有以下错误
无法将“FileStreamDeleteOnClose”类型的对象强制转换为“System.IO.MemoryStream”。
我尝试使用System.IO.Stream,提取工作但不会在屏幕上显示数据,因为“.BinaryWrite(s.ToArray)”不接受方法ToArray。
注意:当我把
#if DEBUG Then
CrystalReportViewer1.ReportSource = relat
CrystalReportViewer1.DataBind ()
Exit Sub
If #End
作品
我需要这个才能在发布模式下工作。
帮帮我
答案 0 :(得分:5)
我找到了解决方案:
https://archive.sap.com/discussions/thread/3322762
正如SAP回答的那样: “这是设计,我们从未完全支持导出到MemoryStream。 唯一的选择是不使用MemoryStream,这不会改变。“
因此,解决方案是将 MemoryStream 替换为 Stream ,并将其发送到 Byte 数组中,如下所示:
Dim strExportFile As String
strExportFile = "ReportReajustesAplicados.pdf"
Dim s As System.IO.Stream = relat.ExportToStream(ExportFormatType.PortableDocFormat)
Dim b(s.Length) As Byte
s.Read(b, 0, CInt(s.Length))
With HttpContext.Current.Response
.ClearContent()
.ClearHeaders()
.ContentType = "application/pdf"
.AddHeader("Content-Disposition", "inline; filename=" & strExportFile)
.BinaryWrite(b)
.Flush()
.SuppressContent = True
HttpContext.Current.ApplicationInstance.CompleteRequest()
End With
答案 1 :(得分:0)
您可以将.BinaryWrite(s.ToArray)
替换为.BinaryWrite(CType(Session("myCrystalReport"), IO.MemoryStream).ToArray())
。
我在下面写了一个例子,我不确定这是否是您正在寻找的,但我希望它可以帮助您。这对我来说是允许用户使用PDF格式下载/查看Crystal Reports。
Dim fileName As String = "ReportReajustesAplicados.pdf"
Response.ContentType = "application/pdf"
Response.Clear()
Response.Buffer = True
Response.AddHeader("Content-Disposition", "filename=" + fileName)
Response.BinaryWrite(CType(Session("myCrystalReport"), IO.MemoryStream).ToArray())
Response.End()
我将Crystal报告放在会话中,包含这些选项(rpt是我的Crystal报告):
Dim options = rpt.ExportOptions
options.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
Session!myCrystalReport= rpt.FormatEngine.ExportToStream(New CrystalDecisions.Shared.ExportRequestContext With {
.ExportInfo = options
})
rpt.Dispose()
如果我的答案不清楚或者不是您要找的,请告诉我,我会尽快更新。
答案 2 :(得分:0)
我在本地运行时遇到了类似的问题。而不是使用流,使用此:
using (CrystalDecisions.CrystalReports.Engine.ReportDocument oRpt =
new ReportDocument())
{
oRpt.Load(Server.MapPath("../RptFiles/MyCReport.rpt"));
oRpt.SetDataSource(MyDataSource);
// ... other stuff you need, params, etc
Response.Clear();
Response.ClearHeaders();
oRpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false,
"MyReportName");
oRpt.Close();
}
答案 3 :(得分:0)
尝试使用此代码进行转换:
//MemoryStream oStream = (MemoryStream)reportDocument.ExportToStream(type);
System.IO.Stream oStream = null;
byte[] byteArray = null;
oStream = reportDocument.ExportToStream(type);
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
以下是完整的代码作为参考:
private void ShowReports(ReportDocument reportDocument, string fileName)
{
string rptType = GetQsSValue("ReportType");
ExportFormatType type = ExportFormatType.PortableDocFormat;
string rptTypeExt = string.Empty;
string contentType = string.Empty;
if (rptType == null || rptType == "PDF")
{
type = ExportFormatType.PortableDocFormat;
rptTypeExt = ".pdf";
contentType = "application/pdf";
}
else if (rptType == "Excel" || rptType == "Text")
{
type = ExportFormatType.Excel;
rptTypeExt = ".xls";
contentType = "application/excel";
}
//MemoryStream oStream = (MemoryStream)reportDocument.ExportToStream(type);
System.IO.Stream oStream = null;
byte[] byteArray = null;
oStream = reportDocument.ExportToStream(type);
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
reportDocument.Close();
reportDocument.Dispose();
reportDocument = null;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + rptTypeExt);
//HttpContext.Current.Response.BinaryWrite(oStream.ToArray());
HttpContext.Current.Response.BinaryWrite(byteArray);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
HttpContext.Current.Response.End();
GC.Collect();
}
答案 4 :(得分:0)
对于那些不使用Web服务器但只需要保存它的人:
Dim myStream As New System.IO.MemoryStream
'ExportToStream acutally produces type: FileStreamDeleteOnClose -- not Stream. Can't implicitly convert it to MemoryStream
Dim myTempStream As System.IO.Stream
myTempStream = oRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat)
'Now copy the data to a MemoryStream before closing the report and destroying the stream.
myTempStream.CopyTo(myStream)
'Clean up and close the temp stream and report.
oRpt.Close()