2

I currently have a Windows Forms ReportViewer that fetches information from an SSRS report.

When the information is fetched I have the option to export them to a PDF, Word or Excel document, to do this, first, I need to save to see the document.

I would rather have it the other way, which is, export the results to a specific file and then, save the document if that's my choice.

Is this possible?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
gmwill934
  • 609
  • 1
  • 10
  • 27

1 Answers1

2

You can handle ReportExport event of ReportViewer and set e.Cancel=true; then using Render method of its LocalReport or ServerReport property, export it to desired location.

Use LocalReport for rdlc reports and ServerReport for rdl reports. In below code I decided to use the property using value of ProcessingMode.

This way, when the user clicks on one of available options in Export button, the report will be exported to the specified format at the location which you set in code:

private void reportViewer1_ReportExport(object sender, 
    Microsoft.Reporting.WinForms.ReportExportEventArgs e)
{
    e.Cancel = true;
    string mimeType;
    string encoding;
    string fileNameExtension;
    string[] streams;
    Microsoft.Reporting.WinForms.Warning[] warnings;

    Microsoft.Reporting.WinForms.Report report;
    if (reportViewer1.ProcessingMode == Microsoft.Reporting.WinForms.ProcessingMode.Local)
        report = reportViewer1.LocalReport;
    else
        report = reportViewer1.ServerReport;

    var bytes = report.Render(e.Extension.Name, e.DeviceInfo,
                    Microsoft.Reporting.WinForms.PageCountMode.Actual, out mimeType,
                    out encoding, out fileNameExtension, out streams, out warnings);

    var path = string.Format(@"d:\file.{0}", fileNameExtension);
    System.IO.File.WriteAllBytes(path, bytes);


    MessageBox.Show(string.Format("Exported to {0}", path));
}

Note: Also don't forget to attach reportViewer1_ReportExport to ReportExport using designer or code, if you forget you will see the dialog.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • By some reason, it still shows the save dialog. I'm using Remote processing mode, does that matter? – gmwill934 Nov 04 '16 at 21:55
  • Yes probably it does matter. But at the moment I don't have access to a Report Server to test a solution for Remote processing mode. BUt for local processing mode, I've tested it and it works properly. – Reza Aghaei Nov 05 '16 at 05:52
  • I checked it using report server too. The only change which you need was using `reportViewer1.ServerReport` instead of `reportViewer1.ServerReport`. I made the change in code. Also don't forget to attach `reportViewer1_ReportExport` to `ReportExport` using designer or code, if you forget you will see the dialog. – Reza Aghaei Nov 05 '16 at 16:47
  • Will that code work if I have no SSRS server and my reports are running using ReportViewer? – Mark Dec 08 '21 at 16:49
  • @Mark, Yes it works. – Reza Aghaei Dec 08 '21 at 22:56
  • OK, good to know. In your code you are showing how to save newly generated file with a specific name. But will save the report on the server, what if I want to download bytes as a stream with a specific name? IS a way of doing it without saving the files on the server? – Mark Dec 09 '21 at 01:55
  • @Mark the example is running in the client app; to improve the code, instead of using a constant file path, just show a SaveFileDialog and let the user choose the file name and save it to that file. – Reza Aghaei Dec 09 '21 at 09:43
  • For cases that you want to export a report from SSRS (server mode) then you can use the reporting service API. Take a look at this example: https://stackoverflow.com/a/40441235/3110834 – Reza Aghaei Dec 09 '21 at 09:45
  • You may also find this post useful (print report without showing preview): https://stackoverflow.com/a/40441235/3110834 – Reza Aghaei Dec 09 '21 at 09:49
  • I think the solution you are pointing at requires SSRS Server. I dont have one. The report is processed in the backend using ReportViewer. – Mark Dec 09 '21 at 10:08
  • Is it a web app or a windows app? If it's a windows app, then just show the modal and choose the folder and save it. If it's a web app, use the solution to generate the file, then write the file to the output stream (response). – Reza Aghaei Dec 09 '21 at 12:24