How to set table caption with OpenXML?

本小妞迷上赌 提交于 2019-12-11 17:39:01

问题


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();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

回答1:


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



来源:https://stackoverflow.com/questions/23863832/how-to-set-table-caption-with-openxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!