簡體   English   中英

表格單元格中的中心文本 - OpenXML SDK

[英]Center text in Table Cell - OpenXML SDK

我創建了一個表格,並在最后一行添加了邊框。

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 控制垂直 alignment:

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

默認情況下,這給出了這個結果:

在此處輸入圖像描述

擴展單元格顯示它現在確實位於中心:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM