8

I am trying to create a table with a header. I want this header to be repeated for each new page that the table takes. How can I do this in C# and 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? 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
RRR
  • 575
  • 1
  • 11
  • 25

4 Answers4

9

As Chris said, an instance of the TableHeader class is what you need. It needs to be appended to the header row's TableRowProperties:

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

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

row.TableRowProperties.AppendChild(new TableHeader());
Michael Csikos
  • 688
  • 9
  • 11
  • 2
    Small addition: with OpenXML SDK2 you have to set the `Val` property of the `TableHeader` object to `OnOffOnlyValues.On`, e.g. `new TableHeader() { Val = OnOffOnlyValues.On }` (see also icalvo's answer). – Chaquotay Aug 25 '15 at 08:52
6

For anyone who is looking for the same issue:

The code below must be applied to the header Row, as TablePropertiesRow

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

tblHeaderRow.AppendChild<TableRowProperties>(tblHeaderRowProps);

Deww!!

icalvo
  • 111
  • 1
  • 3
0

To create header for every table in a page. You need to create multiple body's and append to document.

If you want to create new header to every table, you need to append every table to new body then apply page break.

Finally, append all bodies to document.

Then you finally have your result in created document.

If any doubts reply to me.

Regards, Balaji

0

I think this is what you're looking for. If you apply that element to a particular row, it will behave the way you're describing.

Chris B. Behrens
  • 6,255
  • 8
  • 45
  • 71
  • Can you please expand on -how- one applies that element to a row? The documentation is clear as mud. Thanks. – Lynn Mar 26 '12 at 19:59