应用表格样式

表格样式定义了一组可以轻松应用于表格的格式。可以在表格样式中设置边框、底纹、对齐方式和字体等格式,并将其应用于多个表格以获得一致的外观。

Aspose.Words 支持将表格样式应用于表格,还支持读取任何表格样式的属性。在加载和保存期间通过以下方式保留表格样式:

  • 加载和保存为 DOCX 和 WordML 格式的表格样式时会保留这些格式
  • 以 DOC 格式加载和保存时保留表格样式(但不保留任何其他格式)
  • 导出为其他格式、渲染或打印时,表格样式将扩展为表格中的直接格式,因此保留所有格式

创建表格样式

用户可以创建新样式并将其添加到样式集合中。 Add方法用于创建新的表格样式。

以下代码示例显示如何创建新的用户定义的表格样式:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Name");
builder.InsertCell();
builder.Write("Value");
builder.EndRow();
builder.InsertCell();
builder.InsertCell();
builder.EndTable();
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1");
tableStyle.Borders.LineStyle = LineStyle.Double;
tableStyle.Borders.LineWidth = 1;
tableStyle.LeftPadding = 18;
tableStyle.RightPadding = 18;
tableStyle.TopPadding = 12;
tableStyle.BottomPadding = 12;
table.Style = tableStyle;
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.CreateTableStyle.docx");

复制现有表格样式

如果需要,您可以使用 AddCopy 方法将某个文档中已存在的表格样式复制到样式集合中。

重要的是要知道,通过这种复制,链接的样式也会被复制。

以下代码示例演示如何将样式从一个文档导入到另一个文档:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document srcDoc = new Document();
// Create a custom style for the source document.
Style srcStyle = srcDoc.Styles.Add(StyleType.Paragraph, "MyStyle");
srcStyle.Font.Color = Color.Red;
// Import the source document's custom style into the destination document.
Document dstDoc = new Document();
Style newStyle = dstDoc.Styles.AddCopy(srcStyle);
// The imported style has an appearance identical to its source style.
Assert.AreEqual("MyStyle", newStyle.Name);
Assert.AreEqual(Color.Red.ToArgb(), newStyle.Font.Color.ToArgb());

应用现有表格样式

Aspose.Words提供了一个继承自StyleTableStyle类。 TableStyle 方便用户应用不同的样式选项,如阴影、填充、缩进、CellSpacingFont 等。

此外,Aspose.Words 提供了 StyleCollection 类和 Table 类的一些属性来指定我们将使用哪种表格样式:StyleStyleIdentifierStyleNameStyleOptions

Aspose.Words 还提供 ConditionalStyle 类,表示应用于具有指定表格样式的表格某些区域的特殊格式,以及表示 ConditionalStyle 对象集合的 ConditionalStyleCollection。该集合包含一组永久项目,代表 ConditionalStyleType 枚举类型的每个值的一个项目。 ConditionalStyleType 枚举定义了可以在表格样式中定义条件格式的所有可能的表格区域。

在这种情况下,可以为 ConditionalStyleType 枚举类型下定义的所有可能的表区域定义条件格式。

以下代码示例显示如何定义表标题行的条件格式:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Name");
builder.InsertCell();
builder.Write("Value");
builder.EndRow();
builder.InsertCell();
builder.InsertCell();
builder.EndTable();
TableStyle tableStyle = (TableStyle) doc.Styles.Add(StyleType.Table, "MyTableStyle1");
tableStyle.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.GreenYellow;
tableStyle.ConditionalStyles.FirstRow.Shading.Texture = TextureIndex.TextureNone;
table.Style = tableStyle;
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx");

您还可以选择要应用样式的表格部分,例如第一列、最后一列、带状行。它们列在 TableStyleOptions 枚举中并通过 StyleOptions 属性应用。 TableStyleOptions 枚举允许按位组合这些功能。

以下代码示例演示如何创建应用了表格样式的新表:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
// We must insert at least one row first before setting any table formatting.
builder.InsertCell();
// Set the table style used based on the unique style identifier.
table.StyleIdentifier = StyleIdentifier.MediumShading1Accent1;
// Apply which features should be formatted by the style.
table.StyleOptions =
TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow;
table.AutoFit(AutoFitBehavior.AutoFitToContents);
builder.Writeln("Item");
builder.CellFormat.RightPadding = 40;
builder.InsertCell();
builder.Writeln("Quantity (kg)");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Apples");
builder.InsertCell();
builder.Writeln("20");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Bananas");
builder.InsertCell();
builder.Writeln("40");
builder.EndRow();
builder.InsertCell();
builder.Writeln("Carrots");
builder.InsertCell();
builder.Writeln("50");
builder.EndRow();
doc.Save(ArtifactsDir + "WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx");

下图显示了 Table Styles 在 Microsoft Word 中的表示及其在 Aspose.Words 中的相应属性。

formatting-table-style-aspose-words-net

从表格样式中获取格式并将其应用为直接格式

Aspose.Words 还提供 ExpandTableStylesToDirectFormatting 方法来获取表格样式上的格式并将其扩展为表格的行和单元格作为直接格式。尝试将格式与表格样式和单元格样式结合起来。

以下代码示例演示如何将格式从样式扩展到表行和单元格作为直接格式:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET.git.
Document doc = new Document(MyDir + "Tables.docx");
// Get the first cell of the first table in the document.
Table table = (Table) doc.GetChild(NodeType.Table, 0, true);
Cell firstCell = table.FirstRow.FirstCell;
// First print the color of the cell shading.
// This should be empty as the current shading is stored in the table style.
Color cellShadingBefore = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading before style expansion: " + cellShadingBefore);
doc.ExpandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles.
// A blue background pattern color should have been applied from the table style.
Color cellShadingAfter = firstCell.CellFormat.Shading.BackgroundPatternColor;
Console.WriteLine("Cell shading after style expansion: " + cellShadingAfter);