Winforms ReportViewer 和导出后打开

发布于 2024-12-07 21:10:35 字数 161 浏览 0 评论 0原文

当使用 ReportViewer 中的默认导出按钮时,是否有一种方法可以简单地提示用户打开导出的报表?我查看了 ReportExport 事件,尽管该事件在导出发生之前触发。我唯一能想到的就是取消 ReportExport 并创建我自己的导出功能,尽管我希望我不需要这样做。导出后是否有任何我错过的火灾事件?

When using the default export buttons within the ReportViewer, is there a way to simply prompt the user to open the exported report? I looked at the ReportExport event, though this fires before the export occurs. The only thing I can think of is to cancel the ReportExport and create my own export functionality, though I hope I do not need to do this. Are there any events that I'm missing that fire after the export occurs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月竹挽风 2024-12-14 21:10:35

我找到了一个解决方案。 @KreepN,我在各种讨论板上看到了与您类似的在线解决方案,但是,我找到了另一个更适合我所寻找的解决方案。这提供了所有默认的导出功能。这就是我所做的:

首先,在创建表单时订阅 ReportExport 事件。

this.reportViewer1.ReportExport += new ExportEventHandler(this.ReportViewer1_ReportExport);

这是我的 ReportExport 事件处理方法:

private void ReportViewer1_ReportExport(object sender, ReportExportEventArgs e)
{
    e.Cancel = true;

    string extension = this.GetRenderingExtension(e.Extension);

    SaveFileDialog saveFileDialog = new SaveFileDialog()
    {
        Title = "Save As",
        CheckPathExists = true,
        InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        Filter = e.Extension.LocalizedName + " (*" + extension + ")|*" + extension + "|All files(*.*)|*.*",
        FilterIndex = 0
    };

    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
    {
        this.reportViewer1.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);

        // Here's where I call my method to prompt user to open the file.
        RadExportHelper.OpenFileWithPrompt(saveFileDialog.FileName);                
    }
}

RenderingExtension 类不会公开公开导出的实际文件扩展名,因此我创建了此方法:

private string GetRenderingExtension(RenderingExtension extension)
{
    switch (extension.Name)
    {
        case "PDF":
            return ".pdf";
        case "CSV":
            return ".csv";
        case "EXCEL":
            return ".xls";
        case "MHTML":
            return ".mhtml";
        case "IMAGE":
            return ".tif";
        case "XML":
            return ".xml";
        case "WORD":
            return ".doc";
        case "HTML4.0":
            return ".html";
        case "NULL":
            throw new NotImplementedException("Extension not implemented.");
    }

    throw new NotImplementedException("Extension not implemented.");
}

最后,这是我的辅助方法,用于提示用户并打开文件(如果他们选择):

public static void OpenFileWithPrompt(string file)
{
    if (RadMessageBox.Show(
        Resources.RadHelper_OpenExportedDataMessage,
        Resources.RadHelper_OpenExportedDataTitle,
        MessageBoxButtons.YesNo,
        RadMessageIcon.Question,
        MessageBoxDefaultButton.Button1) == DialogResult.Yes)
    {
        Process.Start(file);
    }
}

I found a solution for this. @KreepN, I had seen similar solutions to yours online throughout various discussion boards, however, I've found another solution which better suites what I was looking for. This provides all of the default functionality for exporting. Here's what I did:

First, subscribe to the ReportExport event when form is created.

this.reportViewer1.ReportExport += new ExportEventHandler(this.ReportViewer1_ReportExport);

Here's my ReportExport event handling method:

private void ReportViewer1_ReportExport(object sender, ReportExportEventArgs e)
{
    e.Cancel = true;

    string extension = this.GetRenderingExtension(e.Extension);

    SaveFileDialog saveFileDialog = new SaveFileDialog()
    {
        Title = "Save As",
        CheckPathExists = true,
        InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        Filter = e.Extension.LocalizedName + " (*" + extension + ")|*" + extension + "|All files(*.*)|*.*",
        FilterIndex = 0
    };

    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
    {
        this.reportViewer1.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);

        // Here's where I call my method to prompt user to open the file.
        RadExportHelper.OpenFileWithPrompt(saveFileDialog.FileName);                
    }
}

The RenderingExtension class doesn't publicly expose the actual file extensions that are exported, so I created this method:

private string GetRenderingExtension(RenderingExtension extension)
{
    switch (extension.Name)
    {
        case "PDF":
            return ".pdf";
        case "CSV":
            return ".csv";
        case "EXCEL":
            return ".xls";
        case "MHTML":
            return ".mhtml";
        case "IMAGE":
            return ".tif";
        case "XML":
            return ".xml";
        case "WORD":
            return ".doc";
        case "HTML4.0":
            return ".html";
        case "NULL":
            throw new NotImplementedException("Extension not implemented.");
    }

    throw new NotImplementedException("Extension not implemented.");
}

Lastly, here's my helper method to prompt the user and open the file if they choose:

public static void OpenFileWithPrompt(string file)
{
    if (RadMessageBox.Show(
        Resources.RadHelper_OpenExportedDataMessage,
        Resources.RadHelper_OpenExportedDataTitle,
        MessageBoxButtons.YesNo,
        RadMessageIcon.Question,
        MessageBoxDefaultButton.Button1) == DialogResult.Yes)
    {
        Process.Start(file);
    }
}
殊姿 2024-12-14 21:10:35

根据各种帖子和资源{12,3},您想要完成的不是Visual Studio 中 ReportViewer 控件的内置功能。

如果此功能是必需的,您可以随时禁用报表查看器上的导出按钮,并添加按钮或其他控件来处理导出。下面是对一个方法的类调用,我在程序中使用该方法在运行报告时自动生成 Excel 文件,但您需要做的唯一更改是通过单击按钮订阅此方法:

旁注: custNmbr 是一个变量,用于根据运行报告的客户来命名报告。如果您愿意,您可以删除它(因为它是我的报告参数),或者通过您自己的代码使其动态化,以确保文件不会相互覆盖。

    public static void reportWriter(ReportViewer reportViewer1, string custNmbr)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtension;

        string Dpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + custNmbr + ".xls";    

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

        using (FileStream fs = new FileStream(Dpath, FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
        }
    }

由于 Dpath 将是这个新导出文件的位置,因此您可以简单地添加对 Excel Interop 的引用并通过以下方式调用 excel/新文件:

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(Dpath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

希望有帮助。

According to a variety of posts and resources {1,2,3}, what you are trying to accomplish is not a built in functionality of the ReportViewer control in Visual Studio.

If this functionality is essential, you could always disable the export button on the report viewer and add a button or other control to take care of the exporting. Below is a class call to a method that I use in a program to auto-generate an excel file when a report is run, but the only change you would have to make would be to subscribe to this method via button click:

Side note: custNmbr is a variable that is used to name the report after the customer it was run for. You may remove this if you like (as it is a report parameter of mine), or make it dynamic via your own code to make sure the files don't overwrite one another.

    public static void reportWriter(ReportViewer reportViewer1, string custNmbr)
    {
        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtension;

        string Dpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + custNmbr + ".xls";    

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

        using (FileStream fs = new FileStream(Dpath, FileMode.Create))
        {
            fs.Write(bytes, 0, bytes.Length);
        }
    }

Since Dpath would be the location of this newly exported file, you could simply Add a reference to the Excel Interop and call excel/the new file via:

Application excel = new Application();
Workbook wb = excel.Workbooks.Open(Dpath, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文