填充设置
颜色和背景图案
Microsoft Excel可以设置单元格的前景(轮廓)和背景(填充)颜色以及背景图案。
Aspose.Cells还以灵活的方式支持这些功能。在本主题中,我们将学习如何使用Aspose.Cells来使用这些功能。
设置颜色和背景图案
Aspose.Cells提供了一个表示Microsoft Excel文件的类,Workbook。Workbook类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。工作表由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 对象的 ForegroundColor 或 BackgroundColor 属性。只有当 Style 对象的 Pattern 属性配置好时,这两个属性才会生效。
- ForegroundColor 属性设置单元格的阴影颜色。 Pattern 属性指定用于前景色或背景色的背景图案类型。Aspose.Cells 提供一个称为 BackgroundType 的枚举,其中包含一组预定义的背景图案类型。
- 如果从 BackgroundType 枚举中选择 BackgroundType.None 值,则不会应用前景色。 同样,如果选择 BackgroundType.None 或 BackgroundType.Solid 值,则不会应用背景色。
- 在检索单元格的阴影/填充颜色时,如果 Style.Pattern 是 BackgroundType.None,Style.ForegroundColor 将返回 Color.Empty。
应用梯度填充效果
要将期望的渐变填充效果应用于单元格,相应地使用 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 提供了一个名为 Workbook 的类,用于表示 Microsoft Excel 文件。 Workbook 类提供了一个 ChangePalette 方法,接受以下参数来添加自定义颜色以修改调色板:
- Custom Color,要添加的自定义颜色。
- Index,自定义颜色在调色板中的索引,将替换指定的颜色。应该在 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"); |