如何使用OpenXML设置表标题? [英] How to set table caption with OpenXML?

查看:284
本文介绍了如何使用OpenXML设置表标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为表格设置标题:

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

但是什么也没发生.怎么了?

But nothing happens. Whats wrong here?

更新

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

推荐答案

在您的代码中,我猜tableDocumentFormat.OpenXml.Wordprocessing.Table.这不是此元素的有效父级. TableCaption应该添加到 TableProperties 元素<w:tblPr>.

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

然后请注意,在2008年版本的ECMA-376中添加了<w:tblCaption>元素,因此Word 2007不支持此元素. TableCaption类仅自Office2010起可用.

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.

要在文字中添加/查看此标题,请执行以下操作:

右键单击表格->属性"->文本/替换"

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

这篇关于如何使用OpenXML设置表标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆