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:
Re
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();
}
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.
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:
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
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);
}
}
}
Choose the relevant Reportviewer control and press F4.
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.)