主要内容

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

mlreportgen.dom.DocumentPart 类

命名空间: mlreportgen.dom

创建文档部件对象

描述

使用 mlreportgen.dom.DocumentPart 类的实例定义文档部件,即报告中的可重复部件。文档部件通常存在一些空位,您需要在报告生成期间填充这些空位。您可以将某个部分追加到文档或相同输出类型的文档部件。

mlreportgen.dom.DocumentPart 类是一个 handle 类。

创建对象

描述

documentPartObj = DocumentPart 使用默认 HTML 模板创建 HTML 文档部件。

documentPart = DocumentPart(type) 根据部件的默认模板创建指定类型的文档部件(例如,Microsoft® Word)。

示例

documentPartObj = DocumentPart(type,templatePath) 根据指定的模板创建文档部件。

documentPartObj = DocumentPart(type,templatePath,docPartTemplateName) 根据指定模板中的指定文档部件模板创建文档部件。

documentPartObj = DocumentPart(templateSrc,docPartTemplateName) 根据存储在指定源使用的模板中的指定文档部件模板创建文档部件。源可以是文档或文档部件。

输入参量

全部展开

输出的类型,指定为下列值之一:

  • "docx" 用于 Microsoft Word

  • "html" 用于 HTML 输出

  • "html-file" 用于单文件 HTML 输出

  • "html-multipage" 用于多页 HTML 输出 (自 R2024a 起)

  • "pdf" 用于 PDF 输出

  • "pdfa" 用于 PDF/A 输出 (自 R2025a 起)

如果使用 templatePath 参量指定模板,则 type 的值必须与模板类型匹配。

此部分的模板文件或文件夹的完整路径,指定为字符向量或字符串标量。如果省略文件扩展名,则模板类型基于文档类型,例如 Word 的 .docx

数据类型: char | string

文档部件模板名称,指定为字符向量或字符串标量。使用 templatePathtemplateSrc 参量指定该部件的存储位置。

数据类型: char | string

文档或文档部件对象,其模板包含此文档部件的模板,指定为文档的 mlreportgen.dom.Document 对象或文档部件的 mlreportgen.dom.DocumentPart 对象。

属性

全部展开

文档中当前空位的 ID,指定为字符向量或字符串标量。

属性:

GetAccess
public
SetAccess
private
Transient
true
NonCopyable
true

数据类型: char | string

当前模板空位的类型,指定为 "Inline""Block"

  • 内联空位适用于段落元素可以包含的文档元素:TextImageLinkTargetExternalLinkInternalLinkCharEntityAutoNumber

  • 块状空位可以包含 ParagraphTableOrderedListUnorderedListDocumentPartGroup 元素。

属性:

GetAccess
public
SetAccess
private
Transient
true
NonCopyable
true

数据类型: char | string

该文档的当前页面布局,指定为 mlreportgen.dom.DOCXPageLayout 对象、mlreportgen.dom.PDFPageLayout 对象或 []。此属性适用于 Word 和 PDF 文档。对于 Word 文档,该值是一个指定当前页面布局的 DOCXPageLayout 对象。对于 PDF 文档,如果文档当前指定了页面布局,则该值为 PDFPageLayout 对象。对于 HTML 文档,该值始终为 []

属性:

GetAccess
public
SetAccess
private
Transient
true
NonCopyable
true

该文档的打开状态,指定为 "unopened""open""closed"

属性:

GetAccess
public
SetAccess
private
Transient
true
NonCopyable
true

数据类型: char | string

如果该模板存储在由本属性中 TemplatePath 指定的模板的文档部件模板库中,则此部件模板的名称;该名称应指定为字符串标量或字符向量。如果此属性的值为 [],则由 TemplatePath 属性指定的模板将作为该部件的模板。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char

此部分模板或模板库中包含此部分模板的模板的路径,指定为字符向量或字符串标量。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char

输出文件类型,指定为以下值之一:

文件类型
"docx"

Microsoft Word

"html"

HTML 输出为压缩或解压后的文件夹,其中包含 HTML 文档文本、图像、样式表和 JavaScript® 文件

"html-file"

HTML 输出由单个文件组成,其中包含报告的文本、样式表、JavaScript 和图像

"html-multipage" (自 R2024a 起)

HTML 输出为压缩或解压后的文件夹,其中包含分为多个页面的 HTML 文档文本、图像、样式表和 JavaScript 文件

"pdf"

PDF

"pdfa" (自 R2025a 起)PDF/A

如果使用 TemplatePath 属性指定模板,则该模板必须与 Type 属性一致。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

此对象的父对象,指定为文档元素对象。一个文档元素必须只有一个父元素。

属性:

GetAccess
public
SetAccess
private
NonCopyable
true

此对象的子对象,指定为文档元素对象数组。此属性包含使用 append 方法追加的文档元素对象。

属性:

GetAccess
public
SetAccess
private
NonCopyable
true

标记,指定为字符向量或字符串标量。DOM API 在创建此对象的过程中生成一个会话唯一标记。生成的标记形式为 CLASS:ID,其中 CLASS 是对象类,ID 是对象的 Id 属性的值。使用此值来帮助确定在文档生成过程中出现的问题的位置。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

目标标识符,指定为字符向量或字符串标量。DOM API 在创建文档元素对象时会生成一个会话唯一标识符。

属性:

GetAccess
public
SetAccess
public
NonCopyable
true

数据类型: char | string

方法

全部展开

示例

全部折叠

此示例创建一个函数 createMagicSquareReport,该函数根据空白文档部件模板定义文档部件。新文档部件有一个标题,其文本取决于输入。生成的每个文档部件都包含一个幻方表,其外观也基于输入。该示例还创建了一个本地函数 createSquareSection,该函数根据输入迭代地将文档部件追加到报告中。

创建函数。

function createMagicSquareReport(square_sizes, report_type)
%createMagicSquareReport Report on magic squares
%    magic_square_report(square_sizes, report_type) 
%    creates a report of the specified output type 
%    (docx, pdf, or html) on the specified magic
%    squares. For example, to create a PDF report on 
%    squares of size 5, 10, and 15, enter the following 
%    line at the MATLAB More About
Append content to  object
You can append the objects in this table to an object.Valid DOM ObjectsValid MATLAB Objects
 mlreportgen.dom.AutoNumber  mlreportgen.dom.CharEntity  mlreportgen.dom.Container  mlreportgen.dom.CustomElement  mlreportgen.dom.DOCXPageLayout  mlreportgen.dom.DOCXSubDoc  mlreportgen.dom.EmbeddedObject  mlreportgen.dom.ExternalLink  mlreportgen.dom.FormalTable  mlreportgen.dom.Group  mlreportgen.dom.Heading  mlreportgen.dom.Heading1  mlreportgen.dom.Heading2  mlreportgen.dom.Heading3  mlreportgen.dom.Heading4  mlreportgen.dom.Heading5  mlreportgen.dom.Heading6  mlreportgen.dom.HorizontalRule  mlreportgen.dom.HTML  mlreportgen.dom.HTMLFile  mlreportgen.dom.Image  mlreportgen.dom.InternalLink  mlreportgen.dom.LineBreak  mlreportgen.dom.LinkTarget  mlreportgen.dom.LOC  mlreportgen.dom.LOF  mlreportgen.dom.LOT  mlreportgen.dom.MATLABTable  mlreportgen.dom.Number  mlreportgen.dom.NumPages  mlreportgen.dom.OrderedList  mlreportgen.dom.Page  mlreportgen.dom.PageBreak  mlreportgen.dom.PageRef  mlreportgen.dom.Paragraph  mlreportgen.dom.Preformatted  mlreportgen.dom.RawText  mlreportgen.dom.StyleRef  mlreportgen.dom.Table  mlreportgen.dom.TemplateHole  mlreportgen.dom.Text  mlreportgen.dom.TOC  mlreportgen.dom.UnorderedList 
 append(DOCXPageFooter,xxx)
DOM2Class autogenerated by script, S. Heile on 13-Nov-2025 11:23:09
 fallback for matlab/doc/src/toolbox/rptgen/ug/xInc/DOMappend/xi_DOMappend_HTMLPage.xml character array  Appends and returns a mlreportgen.dom.Text object.string scalar  Appends and returns a mlreportgen.dom.Text object.numeric variable  Appends and returns a mlreportgen.dom.Number object.logical variable  Appends and returns a mlreportgen.dom.Number object with a value of 1 or 0.1D horizontal array of double values or strings  Appends and returns a mlreportgen.dom.UnorderedList object.2D array of double values or strings  Appends and returns a mlreportgen.dom.Table object.MATLAB table  Appends and returns a mlreportgen.dom.MATLABTable object.
 append(DOCXPageFooter,xxxMLvar)
ML2Class autogenerated by script, S. Heile on 13-Nov-2025 11:23:21
 fallback for matlab/doc/src/toolbox/rptgen/ug/xInc/DOMappend/ML2Class/xi_DOMappend_ML2Class_HTMLPage.xml 
#DOMAppendcommand line:
%
%      createMagicSquareReport([5,10,15],"pdf");

import mlreportgen.dom.*;
rpt = Document("MagicSquareReport",report_type);
open(rpt);
for i = 1:length(square_sizes)
    sz = square_sizes(i);
    section = createSquareSection(rpt,sz);
    append(rpt,section);
end
close(rpt);
rptview(rpt.OutputPath);
 
function section = createSquareSection(rpt,square_size)
import mlreportgen.dom.*;
% Create document part to hold section
section = DocumentPart(rpt.Type);
% Create magic square heading
h1 = Heading1(sprintf("magic(%i)",square_size));
% Put each square on a separate page.
h1.Style = {PageBreakBefore(true)};
append(section,h1);
% Create table to hold square
table = append(section, Table(magic(square_size)));
% Format table
table.Border = "solid";
table.ColSep = "solid";
table.RowSep = "solid";

调用该函数来生成报告。改变输入参量来改变内容或输出格式。本示例创建一个包含三个正方形的 Word 文档。

 createMagicSquareReport([5,8,12],"docx");

详细信息

全部展开

版本历史记录

在 R2014a 中推出

全部展开