我创建了一个 MDI 应用程序,它在不同的子窗体上显示一些 DataGridView,这些子窗体被视为单独的应用程序,因为它们可以访问不同的数据库。
这些数据需要以 PDF 报告的形式导出。我在新的 MDI 子窗体中使用了 ReportViewer 组件,并在解决方案上创建了一个新项目,该项目仅包含 RDLC 报告文件作为嵌入资源,与每个应用程序相关联。出于安全原因,这样做是为了避免在包含每个 .RDLC 文件的程序文件夹中出现用户可访问的文件夹。
这是 VS 解决方案的文件/文件夹结构:
Solution
|
|_ Classes
| |
| |_ Library Project that contains all the classes used for Data Access...
|
|_ MDIMain
| |
| |_ The main application Project that contains some Core Classes and Forms...
|
|_ Reports (A DLL project that doesn't contain .cs code files, but only folders and RDLC reports)
|
|_ App1
| |
| |_ Report1.rdlc
| |_ Report2.rdlc
| |_ ...
|
|_ App2
|
|_ Report1.rdlc
|_ ...
一切正常,但现在我需要通过插入 Logo (即图像)来自定义所有报告。所以我需要向每个报告添加相同的图像。
关注MSDN文档似乎有 3 种可能的图像来源:
- “嵌入”:仅通过将图像嵌入单个报告文件中来工作。但就我而言,由于存储空间的浪费,这不是一个有效的解决方案。
- “外部”:通过指定 URL 或路径(我想做的)。
- “数据库”:指定包含图像的数据库字段。
通过使用 Reports.dll 库作为使用“文件夹.文件名”语法的源,在 ReportViewer 窗口中引用和加载 .rdlc 文件。
/*
* reportSource variable is calculated dynamically with the selection on a ComboBox
* that let the use choose a specific report.
* The source format is like this: "Reports.AppNameFolder.Report1.rdlc"
*/
string reportSource = cmbReport.SelectedValue.ToString();
ReportDataSource rds = new ReportDataSource("");
rds.Name = "DataSet";
rds.Value = _reportDataTable;
Assembly assembly = Assembly.LoadFrom("Reports.dll");
UnmanagedMemoryStream reportStream = (UnmanagedMemoryStream)assembly.GetManifestResourceStream(reportSource);
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.LoadReportDefinition(reportStream);
reportViewer1.LocalReport.DataSources.Add(rds);
reportViewer1.RefreshReport();
所以,如果我想创建一个包含我的图像的文件夹:
- 我应该把它放在哪里,确保文件夹与主项目的输出一起生成?
- 如何在 Debug模式和 Release模式下保持对文件夹和图像的引用,以便程序何时安装到用户客户端?
最佳答案
您可以执行以下操作
在报告的图像属性中
- 图片来源:嵌入式
- 点击导入按钮并加载图片
这样,RDLC会将图片保存为rdlc文件中的base64格式,您可以在解决方案资源管理器面板中使用xml编辑器打开查看,无需担心图片的位置。
另一种方法,例如,如果你想从数据库中加载图像,你可以执行以下操作
- 图片来源:数据库
- 使用此字段:[YourPropertyName]
- 使用这种 MIME 类型:image/jpeg
- 在报表数据集中,添加一个名为 YourPropertyName 的 byte[] 列或属性
最后注意,如果 Logo 在您拥有的所有报告中都很常见,建议添加包含此 Logo 的名为标题的子报告,并将其放入您拥有的每个报告中,如果您这样以后想改logo,只需要在那个header子报表里改就行。
你可以查看这个demo我准备向您展示如何实现这一点(将标题报告添加到您当前的报告中)
希望对你有帮助
关于c# - 如何在多个 RDLC 报告中共享和嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31543720/