错误显示 pdf CrystalReport vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31619138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error show pdf CrystalReport vb.net
提问by Thamires Cunha
I have the code below its function is to load the data from a report on the screen using the CrystalReports.
我有下面的代码,它的功能是使用 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
When I do the extraction of the data.
当我提取数据时。
I have the following error
我有以下错误
Unable to cast object of type 'FileStreamDeleteOnClose' to type 'System.IO.MemoryStream'.
无法将“FileStreamDeleteOnClose”类型的对象转换为“System.IO.MemoryStream”类型。
I tried using System.IO.Stream, extraction works but does not display the data on the screen because ".BinaryWrite (s.ToArray)" does not accept the method ToArray.
我尝试使用 System.IO.Stream,提取工作但不会在屏幕上显示数据,因为“.BinaryWrite (s.ToArray)”不接受 ToArray 方法。
Note: When I put
注意:当我把
#if DEBUG Then
CrystalReportViewer1.ReportSource = relat
CrystalReportViewer1.DataBind ()
Exit Sub
If #End
works
作品
I need this to work but in Release mode.
我需要这个才能工作,但在发布模式下。
Help me
帮我
回答by Matt Roy
I found the solution on:
我在以下位置找到了解决方案:
https://archive.sap.com/discussions/thread/3322762
https://archive.sap.com/discussions/thread/3322762
As answered by SAP guy: "This is by design, we never fully supported export to MemoryStream. Only option is to not use MemoryStream, this will not be changed."
正如 SAP 人员回答的那样:“这是设计使然,我们从未完全支持导出到 MemoryStream。唯一的选择是不使用 MemoryStream,这不会改变。”
So the solution is to replace MemoryStreamwith Streamand send it in Bytearray as in:
所以解决方案是用Stream替换MemoryStream并将其发送到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
回答by Haseeb Ahmed
try to use this code to convert in stream:
尝试使用此代码在流中进行转换:
//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));
here is the full code as reference:
这是完整的代码作为参考:
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();
}
回答by Alan Shaw
For those who are not working with web servers and just need to save it:
对于那些不使用网络服务器而只需要保存它的人:
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()
回答by RHelm
I was having a similar problem when running locally. Instead of using a stream, use this:
我在本地运行时遇到了类似的问题。而不是使用流,使用这个:
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();
}
回答by David Gourde
You could replace .BinaryWrite(s.ToArray)by .BinaryWrite(CType(Session("myCrystalReport"), IO.MemoryStream).ToArray()).
你可以替换.BinaryWrite(s.ToArray)为.BinaryWrite(CType(Session("myCrystalReport"), IO.MemoryStream).ToArray()).
I wrote an example below, I'm not sure if this is what you're looking for, but I hope it can help you. This works for me to let users download/view Crystal Reports using the PDF format.
我在下面写了一个例子,我不确定这是否是你要找的,但我希望它可以帮助你。这对我有用,可以让用户使用 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()
I put the Crystal report in the session with those options (rpt is my Crystal Report):
我将水晶报表放在带有这些选项的会话中(rpt 是我的水晶报表):
Dim options = rpt.ExportOptions
options.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat
Session!myCrystalReport= rpt.FormatEngine.ExportToStream(New CrystalDecisions.Shared.ExportRequestContext With {
.ExportInfo = options
})
rpt.Dispose()
If my answer is unclear or is not what you were looking for, please tell me and I will try to update it as soon as I can.
如果我的回答不清楚或者不是你想要的,请告诉我,我会尽快更新。

