开发者

Winforms ReportViewer and Open After Export

开发者 https://www.devze.com 2023-04-10 07:49 出处:网络
When using the default export buttons with开发者_Go百科in 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

When using the default export buttons with开发者_Go百科in 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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消