Creating a Custom Export to Excel for ReportViewer (rdlc)
Asked Answered
M

4

22

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();

}
Massimo answered 16/8, 2010 at 14:20 Comment(0)
M
23

I put this together based on Microsoft's documentation on ReportViewer and some Google searches in case anyone runs into the issue similar to mine:

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

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

    filename = string.Format("{0}.{1}", "ExportToExcel", "xls");
    Response.ClearHeaders();
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    Response.ContentType = mimeType;
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
Massimo answered 18/8, 2010 at 12:54 Comment(0)
F
39

Just a heads up... the accepted answer will render as an XLS file, which was requested by the original poster.

However, you can now export to XLSX as well. You have to change the format parameter of the Render() method from "Excel" to "EXCELOPENXML".

To get a full list of possible values you can call ReportViewer1.LocalReport.ListRenderingExtensions(). When I ran it on my report viewer instance I got these possible options:

"Excel" "EXCELOPENXML" "IMAGE" "PDF" "WORD" "WORDOPENXML"

I found it very hard to determine what you needed to pass in for the formats. MSDN documents this very poorly if you ask me.

Faker answered 23/4, 2013 at 0:35 Comment(1)
I searched for this answer for so long. Thank you very much.Creswell
M
23

I put this together based on Microsoft's documentation on ReportViewer and some Google searches in case anyone runs into the issue similar to mine:

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

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

    filename = string.Format("{0}.{1}", "ExportToExcel", "xls");
    Response.ClearHeaders();
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    Response.ContentType = mimeType;
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
Massimo answered 18/8, 2010 at 12:54 Comment(0)
U
1

If you want to hide a single export option (though it sounds like you seem to find a custom export useful), here are two options:

Option A. Using CSS to hide the export option:

  1. In your browser's F12 debug window, locate the HTML DOM element for the export, right click it and copy the unique CSS identifier.
  2. Add this to your CSS file (replacing the CSS selector with the context of your clipboard):

    #ReportViewer1_ctl05_ctl04_ctl00_Menu > div:nth-child(3) 
    {
        display:none;
    }
    

Caution is advised when referencing an obscure CSS selector like this, as this is hackish.

Option B. Using code-behind to hide the export option

  1. Add the method below to your .aspx.cs file as back-end code.

    public void DisableUnwantedExportFormat(ReportViewer ReportViewerID, string strFormatName)
    {
        FieldInfo info;
        foreach (RenderingExtension extension in ReportViewerID.ServerReport.ListRenderingExtensions())
        {
            if (extension.Name == strFormatName)
            {
                info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic);
                info.SetValue(extension, false);
            }
        }
    }
    
  2. Choose the relevant Reportviewer control and press F4.

  3. In the Properties window, click the Events icon, then double click the PreRender item to generate the ReportViewer1_PreRender method, I assume your reportViewer control ID is ReportViewer1, edit your method like below:

    protected void ReportViewer1_PreRender(object sender, EventArgs e)
    {
        DisableUnwantedExportFormat((ReportViewer)sender,"Excel");
    }
    

(Source: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/74dad27b-ef7e-4b9b-8922-666b317b3094/how-to-hide-pdf-in-export-option-in-ssrs-reportviewer?forum=sqlreportingservices, and @valik's link only answer.)

Upcoming answered 18/1, 2015 at 2:0 Comment(0)
P
0

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.

Polydactyl answered 25/5, 2021 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.