.net 为 ReportViewer (rdlc) 创建到 Excel 的自定义导出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3494009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:37:36  来源:igfitidea点击:

Creating a Custom Export to Excel for ReportViewer (rdlc)

.netreportviewer

提问by firedrawndagger

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:

我有兴趣在 ReportViewer 中为我的报告创建自定义导出到 Excel 选项。这主要是因为我想取消 pdf,我通过以下方式做到了:

 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.

由于无法在 ReportViewer 中禁用任何特定的导出功能(例如 pdf 但不是 excel)。下面是我(稍微)修改的代码。理想情况下,我想要类似于以前的导出选项的东西,我可以将文件保存到我想要的任何位置。

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

}

回答by JBausmer

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

只是提醒一下……接受的答案将呈现为原始海报要求的 XLS 文件。

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

但是,您现在也可以导出到 XLSX。您必须formatRender()方法的参数从更改"Excel""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:

要获取可能值的完整列表,您可以调用ReportViewer1.LocalReport.ListRenderingExtensions()。当我在我的报告查看器实例上运行它时,我得到了这些可能的选项:

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

"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.

我发现很难确定您需要为格式传递什么。如果你问我,MSDN 的文档非常糟糕。

回答by firedrawndagger

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:

我根据 Microsoft 在 ReportViewer 上的文档和一些 Google 搜索将其放在一起,以防有人遇到与我类似的问题:

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

回答by Jared Thirsk

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:

选项 A. 使用 CSS 隐藏导出选项:

  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;
    }
    
  1. 在浏览器的 F12 调试窗口中,找到要导出的 HTML DOM 元素,右键单击它并复制唯一的 CSS 标识符。
  2. 将此添加到您的 CSS 文件(用剪贴板的上下文替换 CSS 选择器):

    #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.

建议在引用这样一个晦涩的 CSS 选择器时要小心,因为这是 hackish。

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

选项 B. 使用代码隐藏隐藏导出选项

  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");
    }
    
  1. 将下面的方法作为后端代码添加到您的 .aspx.cs 文件中。

    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. 选择相关的 Reportviewer 控件并按 F4。

  3. 在 Properties 窗口中,单击 Events 图标,然后双击 PreRender 项以生成 ReportViewer1_PreRender 方法,我假设您的 reportViewer 控件 ID 是 ReportViewer1,如下编辑您的方法:

    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.)

(来源: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和@valik 的链接只回答。)