1
votes

On an ASP.NET page, when a user is viewing a report from the Crystal Report Viewer(CRV) they have the ability to export the report (eg to PDF). The default filename for the export is the ID of the CRV.

I would like to set the default name to be something that is based on the parameters of the report. (eg "Sales for 2008").

I know I can add a link to the page that would and then I could code up a solution where I generated the PDF in code and the stream it to the browser, but I was hoping there might be a way to do this nativity in Crystal Reports.

4
I believe its the DocumentName property on the reportsam

4 Answers

3
votes
// You could pass the parameters to the web page 
// where you have theCrystalReportViewer control
protected void Page_Load(object sender, EventArgs e)
{
  string reportId = Request["rid"];
  string reportTitle = Request["rtitle"];

  ReportDocument reportDocument = HttpContext.Current.Session[reportId] as ReportDocument;
  this.CommonCrystalReportViewer.ReportSource = reportDocument;
  this.CommonCrystalReportViewer.DataBind();

  // Set the default export file name for the report.
  this.CommonCrystalReportViewer.ID = reportTitle;
}
0
votes

If you are using Visual Studio 2008 to create the report you could edit the ReportClass created to add your DefaultName property.

0
votes

Natively there was a ReportExporter class to be used instead of ReportViewer class but it's not supported anymore. There are some third-parts similar.

I use this code samples:

Get parameters value from report (if you don't have already from Session, QueryString or somewhere else)

string myParamName="XXX";
object myParamValue;
foreach (ParameterField field in reportDocument.ParameterFields)
            {
               if (string.Compare(field.Name.TrimStart('@'), myParamName, true) == 0)
                    myParamValue= field.CurrentValues;
            }

Export using report name needed

string myReportName = "sales for " + myParamValue.ToString() + ".pdf";
try
    {
     reportDocument.ExportToHttpResponse( 
                 ExportFormatType.PortableDocFormat
                 ,Response, true, myReportName);
    }
catch (System.Threading.ThreadAbortException)
    {
        //System.Threading.ThreadAbortException is thrown  
        //because, Response.End is called internally in ExportToHttpResponse method:
    }
-1
votes
protected void Page_Init(object sender, EventArgs e) {
    ...
    // Set the default export file name for the report.
    this.mainReportViewer.ID = reportTitle;
    ...
}

It is mandatory to change reportViewer id in Page_Init function, otherwise will not work.