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

时间:2011-09-07 20:06:23

标签: 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? 

4 个答案:

答案 0 :(得分:7)

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

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

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

row.TableRowProperties.AppendChild(new TableHeader());

答案 1 :(得分:3)

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

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

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

tblHeaderRow.AppendChild<TableRowProperties>(tblHeaderRowProps);

Deww !!

答案 2 :(得分:0)

我认为this正是您所寻找的。如果将该元素应用于特定行,它将按照您描述的方式运行。

答案 3 :(得分:0)

为页面中的每个表创建标题。 您需要创建多个正文并附加到文档。

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

最后,将所有正文附加到文档中。

然后你最终将你的结果放在创建的文档中。

如果有任何疑问回复我。

此致 巴拉吉