Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
951 views
in Technique[技术] by (71.8m points)

c# - ReportViewer - Export report programmatically to a specific location without showing save dialog

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...