應用表格風格

表格樣式定義了一組格式,可以輕鬆地套用到一張桌面上。 格式化(如邊界、填充、對齊和字型)可以在表格樣式中設定並套用到許多資料表上,以產生一致的外觀。

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 提供一個從 Style 類別中繼承的 TableStyleTableStyle 讓使用者可以應用於不同的樣式選項,例如陰影、填充、缩进,以及 CellSpacingFont 等。

此外,Aspose.Words提供StyleCollection類別和一些Table類別的屬性,來指定我們將使用的表格風格:StyleStyleIdentifierStyleNameStyleOptions

Aspose.Words也提供ConditionalStyle這個類別來代表以指定的樣式為條件,某個表格區域所應用的特定格式化、ConditionalStyleCollection來表示由ConditionalStyle物件組成的集合。 這個集合包含一個永久的項目集,每個值分別代表 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);