Winforms ReportViewer and Open After Export

mservidio picture mservidio · Oct 4, 2011 · Viewed 7.7k times · Source

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?

Answer

mservidio picture mservidio · Oct 4, 2011

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