2012-02-16 22 views
0

是否可以通過單擊按鈕(或除reportViewer給出的控件之外的任何其他控件)來觸發導出爲Excel/PDF/Word。我設法通過執行PerformClick來觸發Print和PrintLayout,但它不能用於導出(這是reportViewers ToolBar中的第15個索引,但當我用Export嘗試同樣的事情時,什麼也沒有發生)。這是因爲導出是一個DropDownButton?在那種情況下,我將如何訪問它的項目(我通過名稱和索引號列出了工具欄中的每個項目,列出了Export DropDownButton,但它的項目不是)。在C#WinForms中觸發reportViewer的工具欄功能

謝謝

回答

0

轉換爲Access,你需要像在您發佈的問題上FindToolbar功能的工具欄。這是該函數的C#版本:

private T FindToolStrip<T>(Control control) where T : System.Windows.Forms.Control 
    { 
     if (control == null) 
     { 
      return null; 
     } 
     else if (control is T) 
     { 
      return (T)control; 
     } 
     else 
     { 
      T result = null; 

      foreach (Control embedded in control.Controls) 
      { 
       if (result == null) 
       { 
        result = FindToolStrip<T>(embedded); 
       } 
      } 

      return result; 
     } 
    } 

之後,你創建一個按鈕控件(或其他任何你想要的),並在其中添加以下代碼:

  ToolStrip ts = new ToolStrip(); 
      ts = FindToolStrip<ToolStrip>(this.reportViewer1); 
      ts.Items[13].PerformClick(); 

項目13是打印預覽,您可以通過循環顯示項目名稱(總共有21個,但其中一些是分隔符或標籤)來獲取reportViewers ToolStrip上所有按鈕的索引。你可以使用這段代碼來觸發它們中的任何一個(Export除外,我認爲它與Export是一個下拉按鈕,而不是普通的)有關。如果你有興趣,這是出口顯示的報告爲.xls文件代碼:

  Warning[] warnings; 
      string[] streamids; 
      string mimeType; 
      string encoding; 
      string extension; 
      string path = " "; 
      DialogResult dr= saveFileDialog1.ShowDialog(); 
      path = saveFileDialog1.FileName;    

      byte[] bytes = reportViewer1.LocalReport.Render("Excel", null, out mimeType, out encoding, out extension, out streamids, out warnings); 

      FileStream fs = new FileStream(path + ".xls", FileMode.Create); 
      fs.Write(bytes, 0, bytes.Length); 
      fs.Close(); 

同樣,你可以把它變成一個button_Click方法或類似的東西。我沒有嘗試過與其他擴展名(.pdf或.doc),但我認爲它應該工作得很好。您需要將您要保存的文件的擴展名添加到路徑字符串中,否則您將無法打開創建的文件。 我希望這可以幫助你解決你的問題。

0

我有同樣的問題。您可以發佈一些代碼,以便我可以看到如何訪問工具欄以及「打印和打印預覽」按鈕?也許在看完之後,我也可以弄清其他人。

接受的解決方案對下面的帖子可能會幫助你,如果你可以從VB.NET

Programmatically trigger ReportViewer Toolbar controls