如何使用OpenXml Wordprocessing为每个新页面在表格中创建标题

RRR*_*RRR 5 c# ms-word openxml word-2010

我正在尝试创建一个带有标题的表.我想要为表所采用的每个新页面重复此标题.如何在C#和OpenXml Wordprocessing中执行此操作?

DocumentFormat.OpenXml.Packaging.WordprocessingDocument internalDoc = 
DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open(stream, true);

var tables = wordDoc.MainDocumentPart.Document.Descendants<SdtBlock>().Where
( r => r.SdtProperties.GetFirstChild<Tag>().Val.Value.StartsWith(DATA_TABLE_TAG));

Table table = tables.Descendants<Table>().Single();
//Here can I set some property to repeat the header of the table? 
Run Code Online (Sandbox Code Playgroud)

Mic*_*kos 7

正如Chris所说,TableHeader类的一个实例就是你所需要的.它需要附加到标题行的TableRowProperties:

var row = table.GetFirstChild<TableRow>();

if (row.TableRowProperties == null)
    row.TableRowProperties = new TableRowProperties();

row.TableRowProperties.AppendChild(new TableHeader());
Run Code Online (Sandbox Code Playgroud)

  • 小补充:使用 OpenXML SDK2,您必须将“TableHeader”对象的“Val”属性设置为“OnOffOnlyValues.On”,例如“new TableHeader() { Val = OnOffOnlyValues.On }”(另请参阅 icalvo 的答案)。 (2认同)

小智 6

对于正在寻找相同问题的任何人:

下面的代码必须应用于标题行,如 TablePropertiesRow

TableRowProperties tblHeaderRowProps = new TableRowProperties(
    new CantSplit() { Val = OnOffOnlyValues.On },
    new TableHeader() { Val = OnOffOnlyValues.On }
);

tblHeaderRow.AppendChild<TableRowProperties>(tblHeaderRowProps);
Run Code Online (Sandbox Code Playgroud)

露水!!


Chr*_*ens 0

我想就是你要找的。如果您将该元素应用于特定行,它将按照您所描述的方式运行。