このガイドでは、C# を使用して Word で表をデザインする方法を説明します。IDE の設定方法、手順のリスト、および C# を使用して Word の表スタイルを適用する方法 を示すサンプルコードが含まれています。さまざまなプロパティやメソッドを使用して表のスタイルをカスタマイズする方法について説明します。
C# を使用して Microsoft Word の表デザインを変更する手順
- Aspose.Words for .NET を使用する環境を設定し、表のスタイルを変更可能にする
- Word ファイルを Document オブジェクトにロードし、表を含める
- 表のインデックスを使用して目的の表にアクセスする
- TableStyle クラスを使用して新しい表のスタイルを作成する
- フォント、罫線、フォーマット、条件付きスタイルを設定する
- Style プロパティを使用して表に新しいスタイルを適用する
- 新しい表スタイルを適用した Word ファイルを保存する
これらの手順は、C# を使用して Word で表をデザインする方法 を説明しています。まず Word ファイルを Document オブジェクトにロードし、対象の表を特定して、自動サイズ調整を適用します。次に、新しい TableStyle オブジェクトを作成し、必要なプロパティを設定します。その後、このスタイルを表に適用し、変更を保存します。
C# を使用して Word で表のスタイルを設定するコード
using Aspose.Words; | |
using Aspose.Words.Tables; | |
using System.Drawing; | |
// Apply license | |
License license = new License(); | |
license.SetLicense("license.lic"); | |
// Load the document | |
Document document = new Document("Table.docx"); | |
// Access the first table | |
Table table = (Table)document.GetChild(NodeType.Table, 0, true); | |
// Adjust table to fit within the window | |
table.AutoFit(AutoFitBehavior.AutoFitToWindow); | |
// Define a new table style | |
TableStyle customStyle = (TableStyle)document.Styles.Add(StyleType.Table, "CustomTableStyle"); | |
customStyle.ConditionalStyles.FirstRow.Shading.BackgroundPatternColor = Color.GreenYellow; | |
customStyle.ConditionalStyles.FirstRow.Shading.Texture = TextureIndex.TextureNone; | |
customStyle.Font.Color = Color.Red; | |
customStyle.Borders.LineStyle = LineStyle.Double; | |
customStyle.Borders.LineWidth = 2; | |
customStyle.Font.Shadow = true; | |
customStyle.ParagraphFormat.Alignment = ParagraphAlignment.Left; | |
// Apply the style to the table | |
table.Style = customStyle; | |
// Save the formatted document | |
document.Save("FormattedTable.docx"); |
このコードでは、C# を使用して MS Word の表デザインを変更する方法 を示しています。表のスタイル名を定義し、他の表にも適用できるようにします。既存のスタイルが適用されている場合は、Table.Style
を使用してアクセスし、プロパティを変更してスタイルを更新できます。
この記事では、Word の表スタイルを設定する方法を学びました。既存の表に行を追加する方法については、C# を使用して MS Word 文書の表に行を追加する方法 を参照してください。