2
我目前有一個Windows窗體ReportViewer
,用於從SSRS
報告中獲取信息。ReportViewer - 以編程方式將報告導出到特定位置而不顯示保存對話框
當信息被獲取我必須將它們導出爲PDF選項,Word或Excel文檔,要做到這一點,首先,我需要保存查看文檔。
我寧願用另一種方式,也就是說,將結果導出到一個特定的文件,然後保存該文件,如果這是我的選擇。
這可能嗎?
我目前有一個Windows窗體ReportViewer
,用於從SSRS
報告中獲取信息。ReportViewer - 以編程方式將報告導出到特定位置而不顯示保存對話框
當信息被獲取我必須將它們導出爲PDF選項,Word或Excel文檔,要做到這一點,首先,我需要保存查看文檔。
我寧願用另一種方式,也就是說,將結果導出到一個特定的文件,然後保存該文件,如果這是我的選擇。
這可能嗎?
您可以處理ReportExport
事件的ReportViewer
並設置e.Cancel=true;
然後利用其LocalReport
或ServerReport
財產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
,如果您忘記了會看到對話框。
由於某種原因,它仍然顯示保存對話框。我正在使用遠程處理模式,這很重要嗎? –
是的,這很重要。但目前我無法訪問Report Server來測試遠程處理模式的解決方案。 BUt爲本地處理模式,我已經測試過它,它工作正常。 –
我也使用報告服務器檢查過它。您需要的唯一更改是使用'reportViewer1.ServerReport'而不是'reportViewer1.ServerReport'。我在代碼中進行了更改。如果您忘記了會看到對話框,請不要忘記使用設計器或代碼將'reportViewer1_ReportExport'附加到'ReportExport'。 –