Table 单元格中的居中文本 - OpenXML SDK

Center text in Table Cell - OpenXML SDK

我创建了一个 Table 并在最后一行添加了边框。

TableCellProperties tcp = new TableCellProperties();

TableCellBorders tcb = new TableCellBorders(
        new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicThinLines) },
        new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Double) });

tcp.Append(tcb);
gCell.Append(tcp);

我的问题是文本“28.329,36 €”离上边框太近了。我想把文字放低一点,这样文字到顶部和底部边框的距离相同。

我怎样才能做到这一点?

您可以使用 TableCellVerticalAlignment class:

控制垂直对齐
TableCellProperties tcp = new TableCellProperties();

TableCellBorders tcb = new TableCellBorders(
        new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicThinLines) },
        new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Double) });

//set the alignment to "Center"
TableCellVerticalAlignment tcVA = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };

tcp.Append(tcb);
//append the TableCellVerticalAlignment instance
tcp.Append(tcVA);
gCell.Append(tcp);

需要注意的一件事是“After”行间距将设置为 8pt,因此上面的内容实际上看起来与默认高度下的内容非常相似:

但是如果你要展开单元格,你会看到它居中(几乎,间距仍然存在,只是稍微向外):

要调整间距,可以使用SpacingBetweenLines class:

TableCellProperties tcp = new TableCellProperties();

TableCellBorders tcb = new TableCellBorders(
        new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.BasicThinLines) },
        new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Double) });

//set the alignment to "Center"
TableCellVerticalAlignment tcVA = new TableCellVerticalAlignment() { Val = TableVerticalAlignmentValues.Center };

tcp.Append(tcb);
tcp.Append(tcVA);
gCell.Append(tcp);

Paragraph para = gCell.AppendChild(new Paragraph());

//Adjust the spacing between lines
ParagraphProperties paraProps = new ParagraphProperties();
SpacingBetweenLines spacing = new SpacingBetweenLines() { After = "0" };
paraProps.SpacingBetweenLines = spacing;
para.ParagraphProperties = paraProps;

Run run = para.AppendChild(new Run());
run.AppendChild(new Text("28.329,46 €"));

默认情况下给出此结果:

展开单元格显示它现在确实位于中心: