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



我正在尝试创建一个表头。我希望这个标题对表所占用的每个新页面重复。我如何在c#和OpenXml文字处理中做到这一点?

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? 

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

var row = table.GetFirstChild<TableRow>();
if (row.TableRowProperties == null)
    row.TableRowProperties = new TableRowProperties();
row.TableRowProperties.AppendChild(new TableHeader());

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

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

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

Deww ! !

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

为页面中的每个表创建页眉。您需要创建多个主体并附加到文档中。

如果你想为每个表创建新的标题,你需要将每个表附加到新的主体,然后应用分页符。

最后,将所有主体追加到document。

最后在创建的文档中得到结果。

如有疑问,请回复我。

问候,巴拉吉

最新更新