将报告导出为PDF时,我们发现线程被异常中止。

以下代码用于将报告导出为PDF。

                    Response.Buffer = true;
                    Response.ClearContent();
                    Response.ClearHeaders();
                    Response.ContentType = "application/pdf";
                    myReportDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, Session["ReportName"].ToString());
                    Response.Flush();
                    Response.Close();

请帮助我如何解决此异常。

最佳答案

SAP解释说:

原因

该问题已被识别并记录在问题报告ID ADAPT00765364下。
该错误可能是由于在Response.End()方法内使用了ExportToHttpResponse()引起的。Reponse.End()导致线程中止是一个已知问题。这是设计使然。
有关更多信息,请参见Microsoft KB312629 Article

解决方法

....
 try
   {
   reportDocument.ExportToHttpResponse(format, Response, true, Page.Title);
   }
 catch (System.Threading.ThreadAbortException)
   {
   }
....

解析度

您可以编写自己的代码以将PDF格式的PDF,Word,Excel等格式的Crystal Report直接导出到浏览器。您必须确保使用适当的内容类型。

将Crystal Report以PDF格式导出到Web浏览器的示例代码
try
{
 boReportDocument.Load(Server.MapPath(@"MyReport.rpt"));
 System.IO.Stream oStream = null;
 byte[] byteArray = null;
 oStream = boReportDocument.ExportToStream (ExportFormatType.PortableDocFormat);
 byteArray = new byte[oStream.Length];
 oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
 Response.ClearContent();
 Response.ClearHeaders();
 Response.ContentType = "application/pdf";
 Response.BinaryWrite(byteArray);
 Response.Flush();
 Response.Close();
 boReportDocument.Close();
 boReportDocument.Dispose();

}
catch (Exception ex)
{
 string s = ex.Message;
}

10-07 19:08
查看更多