塗りつぶし設定

色と背景パターン

Microsoft Excel では、セルの前景(輪郭)と背景(塗りつぶし)の色、および背景パターンを設定できます。

Aspose.Cells もこれらの機能を柔軟にサポートしています。このトピックでは、Aspose.Cells を使用してこれらの機能を使用する方法について学びます。

色と背景パターンの設定

Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供します。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetsコレクションが含まれています。ワークシートはWorksheetクラスで表されます。WorksheetクラスはCellsコレクションを提供します。Cellsコレクション内の各項目はCellクラスのオブジェクトを表します。

Cell には GetStyle および SetStyle メソッドがあり、セルの書式設定を取得および設定するために使用されます。Style クラスには、セルの前景色と背景色を設定するためのプロパティがあります。 Aspose.Cells には、以下で示される一連の事前定義された背景パターンのタイプが含まれる BackgroundType 列挙型があります。

背景パターン 説明
DiagonalCrosshatch 対角線のかすかな格子状のパターンを表します
DiagonalStripe 対角線のストライプパターンを表します
Gray6 6.25% グレーのパターンを表します
Gray12 12.5% グレーのパターンを表します
Gray25 25% グレーのパターンを表します
Gray50 50% グレーのパターンを表します
Gray75 75% グレーのパターンを表します
HorizontalStripe 水平ストライプパターンを表します
None 背景なしを表します
ReverseDiagonalStripe 反対角ストライプパターンを表します
Solid ソリッドパターンを表します
ThickDiagonalCrosshatch 太い斜めクロスハッチパターンを表します
ThinDiagonalCrosshatch 細い斜めクロスハッチパターンを表します
ThinDiagonalStripe 細い斜めストライプパターンを表します
ThinHorizontalCrosshatch 細い水平クロスハッチパターンを表します
ThinHorizontalStripe 細い水平ストライプパターンを表します
ThinReverseDiagonalStripe 細い逆斜めストライプパターンを表します
ThinVerticalStripe 細い垂直ストライプパターンを表します
VerticalStripe 垂直ストライプパターンを表します

以下の例では、A1セルの前景色が設定されていますが、A2は前景色と背景色の両方を垂直ストライプの背景パターンで構成するように設定されています。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Create directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Workbook object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Define a Style and get the A1 cell style
Style style = worksheet.Cells["A1"].GetStyle();
// Setting the foreground color to yellow
style.ForegroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A1 cell
worksheet.Cells["A1"].SetStyle(style);
// Get the A2 cell style
style = worksheet.Cells["A2"].GetStyle();
// Setting the foreground color to blue
style.ForegroundColor = Color.Blue;
// Setting the background color to yellow
style.BackgroundColor = Color.Yellow;
// Setting the background pattern to vertical stripe
style.Pattern = BackgroundType.VerticalStripe;
// Apply the style to A2 cell
worksheet.Cells["A2"].SetStyle(style);
// Saving the Excel file
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003);

重要なこと

グラデーション塗りつぶし効果の適用

セルに希望のグラデーション塗りつぶし効果を適用するには、Style オブジェクトの SetTwoColorGradient メソッドを使用してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Instantiate a new Workbook
Workbook workbook = new Workbook();
// Get the first worksheet (default) in the workbook
Worksheet worksheet = workbook.Worksheets[0];
// Input a value into B3 cell
worksheet.Cells[2, 1].PutValue("test");
// Get the Style of the cell
Style style = worksheet.Cells["B3"].GetStyle();
// Set Gradient pattern on
style.IsGradient = true;
// Specify two color gradient fill effects
style.SetTwoColorGradient(Color.FromArgb(255, 255, 255), Color.FromArgb(79, 129, 189), GradientStyleType.Horizontal, 1);
// Set the color of the text in the cell
style.Font.Color = Color.Red;
// Specify horizontal and vertical alignment settings
style.HorizontalAlignment = TextAlignmentType.Center;
style.VerticalAlignment = TextAlignmentType.Center;
// Apply the style to the cell
worksheet.Cells["B3"].SetStyle(style);
// Set the third row height in pixels
worksheet.Cells.SetRowHeightPixel(2, 53);
// Merge the range of cells (B3:C3)
worksheet.Cells.Merge(2, 1, 1, 2);
// Save the Excel file
workbook.Save(dataDir + "output.xlsx");

色とパレット

パレットとは、画像を作成するために使用可能な色の数です。プレゼンテーションで標準化されたパレットを使用することで、ユーザーは一貫した外観を作成できます。各 Microsoft Excel (97-2003) ファイルには、セル、フォント、グリッド線、グラフィックオブジェクト、塗りつぶし、およびグラフの線に適用できる 56 色のパレットがあります。

Aspose.Cells を使用すると、パレットの既存の色だけでなく、カスタム色も使用できます。カスタム色を使用する前に、まずパレットに色を追加します。

このトピックでは、パレットにカスタム色を追加する方法について説明します。

パレットにカスタム色を追加

Aspose.Cells は Microsoft Excel の 56 色のパレットをサポートしています。パレットに定義されていないカスタム色を使用するには、その色をパレットに追加します。

Aspose.Cells は、Microsoft Excel ファイルを表す Workbook クラスを提供します。Workbook クラスには、パレットを変更するための ChangePalette メソッドがあり、カスタム色を追加するために次のパラメータを取ります:

  • カスタムカラー、追加するカスタムカラー。 カスタムカラーが置き換えるパレット内の色のインデックスです。0〜55の間である必要があります。

以下の例では、カスタムカラー(Orchid)をパレットに追加し、フォントに適用する前に追加します。

// Instantiating an Workbook object
Workbook workbook = new Workbook();
//Checks if a color is in the palette for the spreadsheet.
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding Orchid color to the palette at 55th index
workbook.ChangePalette(Color.Orchid, 55);
Console.WriteLine(workbook.IsColorInPalette(Color.Orchid));
// Adding a new worksheet to the Excel object
int i = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[i];
// Accessing the "A1" cell from the worksheet
Cell cell = worksheet.Cells["A1"];
// Adding some value to the "A1" cell
cell.PutValue("Hello Aspose!");
// Defining new Style object
Style styleObject = workbook.CreateStyle();
// Setting the Orchid (custom) color to the font
styleObject.Font.Color = workbook.Colors[55];
// Applying the style to the cell
cell.SetStyle(styleObject);
// Saving the Excel file
workbook.Save("out.xlsx");