2016-11-03 26 views

回答

1

您可以處理ReportExport事件的ReportViewer並設置e.Cancel=true;然後利用其LocalReportServerReport財產Render方法,它出口所需的位置。

對於rdlc報告使用LocalReport,對於rdl報告使用ServerReport。在下面的代碼中,我決定使用屬性值ProcessingMode

這樣,當在Export按鈕可用選項之一的用戶點擊,該報告將出口到該位置處指定的格式,你在代碼中設置:

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

注:也不要忘記使用設計器或代碼將reportViewer1_ReportExport添加到ReportExport,如果您忘記了會看到對話框。

+0

由於某種原因,它仍然顯示保存對話框。我正在使用遠程處理模式,這很重要嗎? –

+0

是的,這很重要。但目前我無法訪問Report Server來測試遠程處理模式的解決方案。 BUt爲本地處理模式,我已經測試過它,它工作正常。 –

+0

我也使用報告服務器檢查過它。您需要的唯一更改是使用'reportViewer1.ServerReport'而不是'reportViewer1.ServerReport'。我在代碼中進行了更改。如果您忘記了會看到對話框,請不要忘記使用設計器或代碼將'reportViewer1_ReportExport'附加到'ReportExport'。 –

相關問題