如何在多个 RDLC 报告中共享和嵌入图像

How to share and embedded image on many RDLC Reports

我创建了一个 MDI 应用程序,它在不同的子窗体上显示了一些 DataGridView,这些子窗体被视为单独的应用程序,因为它们可以访问不同的数据库。

这些数据需要以 PDF 报告的形式导出。我在新的 MDI 子窗体中使用了 ReportViewer 组件,并在解决方案上创建了一个新项目,该项目仅包含 RDLC 报告文件作为嵌入资源,与每个应用程序相关联。出于安全原因,这样做是为了避免在包含每个 .RDLC 文件的程序文件夹中出现用户可访问的文件夹。

这是 VS 解决方案的 files/folders 结构:

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
    |_ ...

一切正常,但现在我需要通过插入徽标(也就是图像)来自定义我的所有报告。所以我需要在每个报告中添加相同的图像。

查看 MSDN 文档似乎有 3 种可能的图像来源:

.rdlc 文件在 ReportViewer windows 中引用和加载,方法是使用 Reports.dll 库作为源,使用 "folder.filename" 语法。

/* 
 * 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();

因此,如果我想创建一个包含我的图像的文件夹:

  1. 我应该把它放在哪里,确定该文件夹与主项目的输出一起生成?
  2. 如何在调试模式和发布模式下保持对文件夹和图像的引用,以便程序何时安装到用户客户端?

您可以执行以下操作

在报告的图像属性中

  1. 图片来源:嵌入式
  2. 单击导入按钮并加载图像

这样,RDLC会将图像保存为文件rdlc中的base64,您可以在解决方案资源管理器窗格中使用xml编辑器打开查看,无需担心位置图片。

另一种方法,如果你想从数据库中加载图像,例如,你可以执行以下操作

  1. 图片来源:数据库
  2. 使用此字段:[YourPropertyName]
  3. 使用此 MIME 类型:image/jpeg
  4. 在报表数据集中,添加一个 byte[] 列或 属性 名为 YourPropertyName

Last Note,如果徽标在您拥有的所有报告中都很常见,建议添加包含此徽标的名为 header 的子报告,然后将其放入你拥有的每个报告,这样如果你以后想更改徽标,你只需要在那个 header 子报告中更改它。

您可以查看此 demo 我准备向您展示如何实现此目的(将 header 报告添加到您当前的报告中)

希望这对您有所帮助