Creating a Custom Export to Excel for ReportViewer (rdlc)

Viewed 55129

I'm interested in creating a custom Export to Excel option for my Report in ReportViewer. This is mostly because I want pdf disalbed and I did that via:

 ReportViewer1.ShowExportControls = false;

Since there is no way to disable any specific export functionality (e.g. pdf but not excel) in ReportViewer. Here's my (slightly) modified code below. Ideally I would like something similar to the previous Export options where I can save the file to whatever location I want.

EDIT: The code works but how would I need to modify the Filestream so that instead of having the file get saved automatically I can prompt the user so that they can save to whichever location they want?

protected void btnExportExcel_Click(object sender, EventArgs e)
{
    Warning[] warnings;
    string[] streamids;
    string mimeType;
    string encoding;
    string extension;

    byte[] bytes = ReportViewer1.LocalReport.Render(
       "Excel", null, out mimeType, out encoding,
        out extension,
       out streamids, out warnings);

    FileStream fs = new FileStream(@"c:\output.xls",
       FileMode.Create);
    fs.Write(bytes, 0, bytes.Length);
    fs.Close();

}
4 Answers

There is a way to disable a particular export in the report viewer. See below.

Step 1: Add OnPreRender event for the report viewer
Step 2: Inside the ReportViewer_PreRender function add the following code

DisableReportExportType.HideUnwantedExportFormat((ReportViewer)sender, "PDF"); // Disables PDF
DisableReportExportType.HideUnwantedExportFormat((ReportViewer)sender, "WORDOPENXML"); //Disables Word
DisableReportExportType.HideUnwantedExportFormat((ReportViewer)sender, "EXCELOPENXML"); //Disables Excel

Use the above as needed. Hope this is what you are looking for.

Related