2010-08-16 250 views
15

我有興趣在ReportViewer中爲我的報告創建自定義導出到Excel選項。這主要是因爲我想PDF disalbed而我做了通過:爲ReportViewer創建自定義導出到Excel(rdlc)

ReportViewer1.ShowExportControls = false; 

既然沒有辦法在的ReportViewer禁用任何特定的導出功能(例如PDF但不擅長)。這是我的(稍微)修改後的代碼。理想情況下,我希望與之前的導出選項類似,可以將文件保存到我想要的任何位置。

編輯:代碼的作品,但我將如何修改文件流,以便讓文件不會自動保存我可以提示用戶,以便他們可以保存到他們想要的位置?

protected void btnExportExcel_Click(object sender, EventArgs e) 
{ 
    Warning[] warnings; 
    string[] streamids; 
    string mimeType; 
    string encoding; 
    string extension; 

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

    FileStream fs = new FileStream(@"c:\output.xls", 
     FileMode.Create); 
    fs.Write(bytes, 0, bytes.Length); 
    fs.Close(); 

} 

回答

18

我把這個一起基於微軟的的ReportViewer和萬一有人跑進類似地雷問題的一些谷歌搜索文檔:

protected void ExportExcel_Click(object sender, EventArgs e) 
{ 
    Warning[] warnings; 
    string[] streamids; 
    string mimeType; 
    string encoding; 
    string extension; 
    string filename; 

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

    filename = string.Format("{0}.{1}", "ExportToExcel", "xls"); 
    Response.ClearHeaders(); 
    Response.Clear(); 
    Response.AddHeader("Content-Disposition", "attachment;filename=" + filename); 
    Response.ContentType = mimeType; 
    Response.BinaryWrite(bytes); 
    Response.Flush(); 
    Response.End(); 
} 
26

剛擡起頭...公認的答案會渲染爲原始海報請求的XLS文件。

但是,您現在也可以導出到XLSX。您必須將Render()方法的format參數從"Excel"更改爲"EXCELOPENXML"

要獲得完整的可能值列表,您可以撥打ReportViewer1.LocalReport.ListRenderingExtensions()。當我跑在我的報表查看器實例中我得到了這些可能的選項:

"Excel" "EXCELOPENXML" "IMAGE" "PDF" "WORD" "WORDOPENXML"

我發現它很難確定你需要什麼樣的傳遞中的格式。如果你問我,MSDN文檔很糟糕。

+1

我搜索了這麼久的答案。非常感謝你。 – 2014-04-24 13:12:01

0

如果你想(像你似乎找到了一個自定義的出口有用的,它聽起來)隱藏一個導出選項,這裏有兩種選擇:

選項A.使用CSS來隱藏導出選項:

  1. 在瀏覽器的F12調試窗口中,找到導出的HTML DOM元素,右鍵單擊它並複製唯一的CSS標識符。引用一個不起眼的CSS選擇器這樣當

    #ReportViewer1_ctl05_ctl04_ctl00_Menu > div:nth-child(3) 
    { 
        display:none; 
    } 
    

警告的是,因爲這是hackish的:

  • 添加到您的CSS文件(用您的剪貼板的情況下更換CSS選擇器)。

    選項B.使用代碼隱藏隱藏導出選項

    1. 添加下面的方法到您的.aspx。cs文件作爲後端代碼。

      public void DisableUnwantedExportFormat(ReportViewer ReportViewerID, string strFormatName) 
      { 
          FieldInfo info; 
          foreach (RenderingExtension extension in ReportViewerID.ServerReport.ListRenderingExtensions()) 
          { 
           if (extension.Name == strFormatName) 
           { 
            info = extension.GetType().GetField("m_isVisible", BindingFlags.Instance | BindingFlags.NonPublic); 
            info.SetValue(extension, false); 
           } 
          } 
      } 
      
    2. 選擇相關的Reportviewer控件並按F4。

    3. 在屬性窗口,單擊事件圖標,然後雙擊預渲染項目生成ReportViewer1_PreRender方法,我想你的ReportViewer控件ID是ReportViewer1,編輯方法如下圖所示:

      protected void ReportViewer1_PreRender(object sender, EventArgs e) 
      { 
          DisableUnwantedExportFormat((ReportViewer)sender,"Excel"); 
      } 
      

    (來源:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/74dad27b-ef7e-4b9b-8922-666b317b3094/how-to-hide-pdf-in-export-option-in-ssrs-reportviewer?forum=sqlreportingservices,和@ valik的鏈接唯一的答案。)