0

The code I'm troubleshooting exports a Crystal Report ReportDocument to Excel. Most of the time, the export works just fine. Unfortunately for some datasets, the ExportToHttpResponse method never returns and causes the app to hang. Eventually there is a Thread was being aborted exception along with a request timeout.

Here is the line that hangs:

reportDocument.ExportToHttpResponse(ExportFormatType.Excel,Response,True, fileName);

I also tried using ExportToStream from here which also hangs:

System.IO.Stream myStream;
byte[] byteArray;
myStream = boReportDocument.ExportToStream (ExportFormatType.PortableDocFormat);

I have tried different export formats, restarting IIS, etc. There seems to be a size limit or perhaps specific data scenarios that cause these methods to hang. Any workarounds or explanations for this behavior? Thanks!

Community
  • 1
  • 1
James Lawruk
  • 30,112
  • 19
  • 130
  • 137

1 Answers1

0

Try this:

   Public Shared Sub ExportDataSetToExcel(ByVal ds As DataTable, ByVal filename As String)
        Dim response As HttpResponse = HttpContext.Current.Response
        response.Clear()
        response.Buffer = True
        response.Charset = ""
        'response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        response.ContentType = "application/vnd.ms-excel"
        'response.AddHeader("Content-Disposition", "attachment;filename=""" & filename & ".xls")

        Using sw As New StringWriter()
            Using htw As New HtmlTextWriter(sw)
                Dim dg As New DataGrid()
                dg.DataSource = ds
                dg.DataBind()
                dg.RenderControl(htw)
                response.Charset = "UTF-8"
                response.ContentEncoding = System.Text.Encoding.UTF8
                response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble())
                response.Output.Write(sw.ToString())
                response.[End]()
            End Using
        End Using
    End Sub

and in your viewer add :

DT = New DataTable
DT = (Your Method)
ExportDataSetToExcel(DT, "ExportedReport")

also add :

 Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
    ReportObject.Close()
    ReportObject.Dispose()
End Sub

So report would not add restrictions on the number of loaded reports.

Abdulrahman_88
  • 545
  • 1
  • 5
  • 15