以下是我使用 OpenXML (C#) 在 docx 中创建测试表的边框格式。
//Create a TableProperties object and specify its border information.
var borderType = BorderValues.Single;
DocumentFormat.OpenXml.UInt32Value size = 5;
TableProperties tblProp = new TableProperties(
new TableJustification() { Val = TableRowAlignmentValues.Center },
new TableBorders(
new TopBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
new BottomBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
new LeftBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
new RightBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
new InsideHorizontalBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size },
new InsideVerticalBorder(){ Val = new EnumValue<BorderValues>(borderType), Size = size }
)
);
// Append the TableProperties object to the empty table.
table.AppendChild<TableProperties>(tblProp);
上面的格式化产生了这样的结果(当在365word查看器中查看时)。 问题是,为什么?
EDIT: 它在word桌面程序中正确显示... 所以这似乎是一个#微软的错误。
不同的客户端往往对该规范的支持程度不同。我记得当我试图为Google docs制作内容时,我就在与此作斗争。
有一些事情你可以尝试。
首先,不要使用 LeftBorder
和 RightBorder
试试 StartBorder
和 EndBorder
. 这些对于支持RTL文化与LTR文化中的语义考虑很重要,因此可能会得到更好的支持。
其次,逆向工程对我来说几乎总是有效的。先在Word或其他工具中按照你想要的方式来设计文档的样式,然后提取并检查XML。
我想你会发现,Word定义了一种风格,它应用于表格,而不是试图在行内完成所有的工作--复制这种方法比较复杂,但往往能起到作用。