I am exporting a report and I have noticed that I am copying a lot of code for each report. I would like to put it in a method in a separate class, but I am unsure of how to do the instantiation piece after some research. My code is below:
ActiveReport rpt = new Reports.rptContractListing_Merchant(); rpt.Run(); try { rpt.Run(false); } catch (DataDynamics.ActiveReports.ReportException eRunReport) { // Failure running report, just report the error to the user: Response.Clear(); Response.Write("<h1>Error running report:</h1>"); Response.Write(eRunReport.ToString()); return; } XlsExport xls = new XlsExport(); xls.MinColumnWidth = (float)0.5; xls.Export(rpt.Document, m_stream); m_stream.Position = 0; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("content-disposition", "inline; filename=ContractListing_Merchant.xls"); System.IO.MemoryStream m_stream = new System.IO.MemoryStream(); Response.BinaryWrite(m_stream.ToArray()); Response.End(); Here is the part I am unsure with the reflection:
ActiveReport rpt = new Reports.rptContractListing_Merchant(); Another Example:
ActiveReport rpt = new Reports.rptContractDetails(); try { rpt.Run(false); } catch (DataDynamics.ActiveReports.ReportException eRunReport) { // Failure running report, just report the error to the user: Response.Clear(); Response.Write("<h1>Error running report:</h1>"); Response.Write(eRunReport.ToString()); return; } Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "inline; filename=ContractDetails.pdf"); PdfExport pdf = new PdfExport(); System.IO.MemoryStream memStream = new System.IO.MemoryStream(); pdf.Export(rpt.Document, memStream); Response.BinaryWrite(memStream.ToArray()); Response.End();
Reports.rptContractListing_Merchant, instiates one then runs the rest of this function?return File(...)method on the controller that'll accept the stream and a filename and do this all for you.