主要内容

本页采用了机器翻译。点击此处可查看英文原文。

创建定义自定义页眉的报告

此示例说明如何创建一个在每一页上都包含自定义页眉值的报告。在此示例中,您定义一个自定义 mlreportgen.report.Report 类和一个包含页眉空位的自定义模板。然后,您更新该类以引用自定义模板和空位,并使用该类生成一个填充空位的 PDF 报告。

创建自定义报告类

要创建一个可用于定义页眉信息的自定义报告类,请使用 mlreportgen.report.Report.customizeReport 方法。在文件夹 @MyClass 中指定 mlreportgen.report.Report 类及其模板。

mlreportgen.report.Report.customizeReport("@MyReport");
mlreportgen.report.Report.customizeReport 方法在 @MyReport 文件夹中创建一个名为 MyReport 的类定义文件,并将每种报告类型的模板文件存储在 @MyReport\resources\templates 文件夹中。

创建自定义模板

mlreportgen.report.Report.customizeReport 方法根据默认 mlreportgen.report.Report 类使用的默认模板创建 HTML、PDF 和 Microsoft® Word 模板。在此示例中,您将创建一个自定义 PDF 模板来定义带有空位的页眉。

为简化创建模板的代码,请导入 mlreportgen.dom 命名空间并指定旧模板和新模板的文件夹路径。

import mlreportgen.dom.*
templateBasePath = "@MyReport/resources/templates";
templateType = "pdf";
templateName = "default";
oldTemplatePath = fullfile(templateBasePath, ...
 templateType,templateName);
newTemplatePath = oldTemplatePath+"_withHeader";

基于默认模板创建一个 mlreportgen.dom.Template 对象。

template = Template(newTemplatePath,templateType,oldTemplatePath);
open(template);

为模板创建页眉。"default" 输入参量值指定报告在每一页上都包含页眉。有关详细信息,请参阅 PageType

header = PDFPageHeader("default");

定义页眉内容。在此示例中,您定义的空位将用客户名称和安全级别来填充。包含静态内容 Client: Security Level: ,然后在每段静态内容旁边添加一个空位。

clientName = Paragraph("Client: ");
clientName.WhiteSpace = "preserve";
append(clientName,TemplateHole("Client"))
securityPara = Paragraph("Security Level: ");
securityPara.WhiteSpace = "preserve";
append(securityPara,TemplateHole("SecurityLevel"));

要格式化页眉内容,请创建一个定义内容布局的不可见表,然后将该表追加到页眉中。

tbl = Table([clientName,securityPara]);
tbl.Width = "100%";
tbl.TableEntriesVAlign = "middle";
rightEntry = tbl.entry(1,2);
rightEntry.Style = {HAlign("right")};
append(header, tbl);
append(header, HorizontalRule);
template.CurrentPageLayout.PageHeaders = header;

关闭并预览模板。模板包含静态内容。尖括号表示空位。

close(template);
tmplview(template);

The example pdf report template. The header includes the Client and Security Level fields in angle brackets. The Client field is on the left, and the Security Level field is on the right.

修改自定义报告

接下来,通过在类中将空位指定为属性,更新自定义 mlreportgen.report.Report 类以使用自定义模板。在 @MyReport 文件夹中,打开 MyReport 类定义文件。更新 properties 定义部分,列出 ClientSecurityLevel

properties
  Client
  SecurityLevel
end 

类定义文件中的 getDefaultTemplatePath 方法检索报告的模板。更新此方法以使用新的 PDF 模板,但对其他支持的文档类型使用默认值。

function templatePath = getDefaultTemplatePath(rpt)         
  templateBasePath = fullfile(fileparts(mfilename("fullpath")), ...
  "resources","templates",rpt.Type);
    switch lower(rpt.Type)
      case "pdf"
        templatePath = fullfile(templateBasePath, ...
         "default_withHeader.pdftx");
      case "docx"
        templatePath = fullfile(templateBasePath,"default.dotx");
      case "html"
        templatePath = fullfile(templateBasePath,"default.htmtx");
      otherwise
         error("Unsupported report type: %s", type);
    end
end

生成示例报告

接下来,您可以生成具有自定义页眉内容的报告。

import mlreportgen.report.*
rpt = MyReport("test","pdf");

要填充空位内容,请将内容分配给您添加到自定义 mlreportgen.report.Report 类中的 ClientSecurityLevel 属性。

rpt.Client = "John Doe";
rpt.SecurityLevel = "Top Secret";

然后,向报告中添加内容或其他报告器。具有 Layout 属性的报告器默认定义自己的布局,该布局会覆盖报告布局。您也可以通过将报告器的 UseReportLayout 属性设置为 true 来指定这些报告器使用包含它们的父报告的布局。在此示例中,添加一个标题页,并将标题页的布局设置为自定义报告的布局。

tp = TitlePage(Title="Report for John Doe");
tp.Layout.UseReportLayout = true;
append(rpt,tp);

添加目录、章节以及该章节的示例内容,并将它们追加到报告。

toc = TableOfContents;
toc.Layout.UseReportLayout = true;
append(rpt,toc);
ch = Chapter("Example Chapter");
ch.Layout.UseReportLayout = true;
append(ch,"Example content");
append(rpt,ch);

查看报告以查看结果。

close(rpt);
rptview(rpt);

每一页的页眉均包含静态内容和填充的空位。

The example pdf report. This shows the page that contains the chapter. The header includes the Client and Security Level fields, as well as the content that you entered. The Client field is on the left, and the Security Level field is on the right.

另请参阅

|

主题