2008-10-31 69 views
38

我有一個由Windows服務和表單應用程序使用的報告。所以,我想把報告嵌入到一個可以被兩者使用的DLL文件中。顯示嵌入在DLL文件中的.RDLC報告

問題是,如果我嘗試在Windows窗體應用程序中設置ReportViewer控件的ReportEmbeddedResource屬性,它將搜索Windows窗體應用程序中的資源而不是dll文件。

例如: - 代碼從Windows窗體應用程序:

rv.LocalReport.ReportEmbeddedResource = "MyReportInMyDLLFile.rdlc" 

我怎樣才能使上面的命令看在我的DLL文件嵌入的資源?

回答

53

這樣的事情應該這樣做:

Assembly assembly = Assembly.LoadFrom("Reports.dll"); 
Stream stream = assembly.GetManifestResourceStream("Reports.MyReport.rdlc"); 
reportViewer.LocalReport.LoadReportDefinition(stream); 
+8

我愛你。 – 2011-04-15 15:34:12

+1

當我在SharePoint 2010中的`application page`中使用上述代碼時,出現以下錯誤:`無法加載文件或程序集'文件:/// c:\ windows \ system32 \ inetsrv \ Reports.dll'或其某個依賴項。系統找不到指定的文件,但它在WinForm應用程序中可用。 – Amir 2014-03-17 03:52:09

10

可能最好的做法是從其他程序集中獲取RDLC資源的流,然後將其傳遞給Report Viewer控件的「LoadReportDefinition」方法。如何從嵌入的資源在不同的組件中獲取流

詳細信息可以在這裏找到:Retrieving Resources with the ResourceManager Class

此外,您需要使用它的完整的命名空間路徑指嵌入的資源。

E.g.如果你有該app的默認命名空間的應用程序,和你保持一個文件夾名爲「MyReport.rdlc」報道稱「報告」,該報告引用調用將是: -

rv.LocalReport.ReportEmbeddedResource = "TheApp.Reports.MyReport.rdlc"; 
22

只要使用組件的完整的命名空間,則文件夾名稱,然後將該文件的名稱:

rv.LocalReport.ReportEmbeddedResource = 
    "My.Assembly.Namespace.Folder1.Folder2.MyReport.rdlc"; 

然後確保使用屬性窗格將報告文件設置爲嵌入式資源。