[英]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();
}
}
}
}
}
}
}
In your code, I guess the table
is a DocumentFormat.OpenXml.Wordprocessing.Table
. 在您的代码中,我猜该table
是DocumentFormat.OpenXml.Wordprocessing.Table
。 This is not a valid parent for this element. 这不是此元素的有效父级。 The TableCaption
should be added to a TableProperties
element <w:tblPr>
. TableCaption
应该添加到TableProperties
元素<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. 然后请注意,在2008年版本的ECMA-376中添加了<w:tblCaption>
元素,因此Word 2007不支持此元素。 The TableCaption
class is only available since Office2010. TableCaption
类仅自Office2010起可用。
To add/see this caption in word: 要添加/查看此标题,请执行以下操作:
Right-Click on the table->Properties->Text/Replacement 右键单击表格->属性->文本/替换
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.