Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

c# - How to set table caption with OpenXML?

I want to set caption to my table:

var caption = new TableCaption();
caption.Val = "Test";
table.Append(caption);

But nothing happens. Whats wrong here?

UPADATE

                foreach (var tabl in mainPart.Document.Body.Descendants<Table>())
                {
                    foreach (var trow in tabl.Elements<TableRow>())
                    {
                        foreach (var tcell in trow.Elements<TableCell>())
                        {
                            foreach (var para in tcell.Elements<Paragraph>())
                            {
                                foreach (var run in para.Elements<Run>())
                                {
                                    foreach (var text in run.Elements<Text>())
                                    {
                                        if (
                                            text.Text ==
                                                "my caption")
                                        {
                                            para.RemoveAllChildren();
                                            para.Remove();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your code, I guess the table is a DocumentFormat.OpenXml.Wordprocessing.Table. This is not a valid parent for this element. The TableCaption should be added to a TableProperties element <w:tblPr>.

Table table = new Table();
TableProperties tableProperties = new TableProperties(
                  new TableCaption() { Val = "Test" }
                );
table.AppendChild<TableProperties>(tableProperties);

Then note the <w:tblCaption> element was added in the 2008 version of ECMA-376, so Word 2007 does not support this. The TableCaption class is only available since Office2010.

To add/see this caption in word:

Right-Click on the table->Properties->Text/Replacement


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...