应用表格样式

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

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

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

创建表格样式

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

下面的代码示例演示如何创建新的用户定义表样式:

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"Value");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->get_Borders()->set_LineStyle(LineStyle::Double);
tableStyle->get_Borders()->set_LineWidth(1);
tableStyle->set_LeftPadding(18);
tableStyle->set_RightPadding(18);
tableStyle->set_TopPadding(12);
tableStyle->set_BottomPadding(12);
table->set_Style(tableStyle);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.CreateTableStyle.docx");

复制现有表格样式

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

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

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

EXAMPLE

应用现有表格样式

Aspose.Words提供从Style类继承的TableStyleTableStyle方便用户应用不同的样式选项,如阴影、填充、缩进、CellSpacingFont等。

此外,Aspose.Words提供了StyleCollection类和Table类的一些属性,以指定我们将使用哪种表样式: Style, StyleIdentifier, StyleName, 和StyleOptions

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-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<Table> table = builder->StartTable();
builder->InsertCell();
builder->Write(u"Name");
builder->InsertCell();
builder->Write(u"Value");
builder->EndRow();
builder->InsertCell();
builder->InsertCell();
builder->EndTable();
auto tableStyle = System::ExplicitCast<TableStyle>(doc->get_Styles()->Add(StyleType::Table, u"MyTableStyle1"));
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_BackgroundPatternColor(System::Drawing::Color::get_GreenYellow());
tableStyle->get_ConditionalStyles()->get_FirstRow()->get_Shading()->set_Texture(TextureIndex::TextureNone);
table->set_Style(tableStyle);
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.DefineConditionalFormatting.docx");

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
SharedPtr<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->set_StyleIdentifier(StyleIdentifier::MediumShading1Accent1);
// Apply which features should be formatted by the style.
table->set_StyleOptions(TableStyleOptions::FirstColumn | TableStyleOptions::RowBands | TableStyleOptions::FirstRow);
table->AutoFit(AutoFitBehavior::AutoFitToContents);
builder->Writeln(u"Item");
builder->get_CellFormat()->set_RightPadding(40);
builder->InsertCell();
builder->Writeln(u"Quantity (kg)");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Apples");
builder->InsertCell();
builder->Writeln(u"20");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Bananas");
builder->InsertCell();
builder->Writeln(u"40");
builder->EndRow();
builder->InsertCell();
builder->Writeln(u"Carrots");
builder->InsertCell();
builder->Writeln(u"50");
builder->EndRow();
doc->Save(ArtifactsDir + u"WorkingWithTableStylesAndFormatting.BuildTableWithStyle.docx");

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

formatting-table-style-aspose-words-cpp

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git.
auto doc = MakeObject<Document>(MyDir + u"Tables.docx");
// Get the first cell of the first table in the document.
auto table = System::ExplicitCast<Table>(doc->GetChild(NodeType::Table, 0, true));
SharedPtr<Cell> firstCell = table->get_FirstRow()->get_FirstCell();
// First print the color of the cell shading.
// This should be empty as the current shading is stored in the table style.
System::Drawing::Color cellShadingBefore = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor();
std::cout << (String(u"Cell shading before style expansion: ") + cellShadingBefore) << std::endl;
doc->ExpandTableStylesToDirectFormatting();
// Now print the cell shading after expanding table styles.
// A blue background pattern color should have been applied from the table style.
System::Drawing::Color cellShadingAfter = firstCell->get_CellFormat()->get_Shading()->get_BackgroundPatternColor();
std::cout << (String(u"Cell shading after style expansion: ") + cellShadingAfter) << std::endl;