填充设置

颜色和背景图案

Microsoft Excel 可以设置单元格的前景(轮廓)和背景(填充)颜色和背景图案。

Aspose.Cells 也以灵活的方式支持这些功能。在本主题中,我们通过 Aspose.Cells 学习使用这些功能。

设置颜色和背景图案

Aspose.Cells提供了一个类,工作簿表示 Microsoft Excel 文件。这工作簿类包含一个工作表允许访问 Excel 文件中每个工作表的集合。工作表由工作表班级。这工作表类提供了Cells收藏。中的每一项Cells集合代表一个对象Cell班级。

Cell获取样式设置样式用于获取和设置单元格格式的方法。这风格类提供用于设置单元格前景色和背景色的属性。 Aspose.Cells提供了背景类型包含一组预定义类型的背景图案的枚举,如下所示。

背景图案 描述
对角线交叉影线 表示对角交叉影线图案
斜条纹 代表斜条纹图案
灰色6 代表 6.25% 灰色图案
灰12 代表 12.5% 灰色图案
灰25 代表 25% 灰色图案
灰色50 代表 50% 灰色图案
灰色75 代表75%灰色图案
水平条纹 代表横条纹图案
没有任何 代表没有背景
反斜条纹 表示反斜条纹图案
坚硬的 表示实心图案
粗斜线 表示粗对角交叉影线图案
细对角线 表示细对角线交叉影线图案
细斜条纹 代表细斜条纹图案
细水平剖面线 表示细水平交叉影线图案
细横条纹 代表细横条纹图案
细反斜条纹 代表细反斜条纹图案
细竖条纹 代表细竖条纹图案
垂直条纹 代表竖条纹图案

在下面的示例中,设置了 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);

重要须知

应用渐变填充效果

要将所需的渐变填充效果应用于单元格,请使用风格对象的设置双色渐变相应的方法。

// 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 文件。这工作簿类提供了改变调色板采用以下参数添加自定义颜色以修改调色板的方法:

  • Custom Color,要添加的自定义颜色。
  • 索引,自定义颜色将替换的调色板中颜色的索引。应该在 0-55 之间。

下面的示例在将自定义颜色(兰花)应用于字体之前将其添加到调色板。

// 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");