Previous topicNext topic
Help > 开发指南 > Excel > API > 示例 > 设置单元格格式 >
如何:更改单元格字体和背景颜色

您可以更改单元格字体颜色和单元格背景。

若要为单个单元格指定这些属性,请修改 Cell 对象的 Formatting.Font 和 Formatting.Fill 属性,这些属性继承自 Formatting 接口。

若要更改单元格区域的颜色特征,请调用此区域的 CellRange.BeginUpdateFormatting 方法,修改返回的 Formatting 对象的 Font 和 Fill 属性,并调用 CellRange.EndUpdateFormatting 方法以完成修改。

单元格字体颜色

Formatting.Font 属性返回 SpreadsheetFont 对象。将此对象的 SpreadsheetFont.Color 属性设置为所需的 Color  值以更改单元格字体颜色。

单元格背景

Formatting.Fill 属性返回 Fill 对象。使用此对象的以下属性来设置单元格背景。

若要在单个步骤中与多个单元格共享字体颜色和背景设置,请使用根据需要指定的 Formatting.Font 和 Formatting.Fill 属性创建或修改样式,并将此样式分配给所需单元格的 CellRange.Style。有关如何创建和修改样式的信息,请参阅以下主题: 如何:创建或修改单元格样式

示例

以下示例指定单个单元格和单元格区域的字体和背景色:

Vb.Net
'设置单个单元格的格式。
worksheet.Cells("A1").Font.Color = Color.Red
worksheet.Cells("A1").FillColor = Color.Yellow

'设置单元格区域的格式。
Dim range As DevExpress.Spreadsheet.CellRange = worksheet.Range("C3:D4")
Dim rangeFormatting As DevExpress.Spreadsheet.Formatting = range.BeginUpdateFormatting()
rangeFormatting.Font.Color = Color.Blue
rangeFormatting.Fill.BackgroundColor = Color.LightBlue
rangeFormatting.Fill.PatternType = DevExpress.Spreadsheet.PatternType.LightHorizontal
rangeFormatting.Fill.PatternColor = Color.Violet
range.EndUpdateFormatting(rangeFormatting)

C#
// 设置单个单元格的格式。
worksheet.Cells["A1"].Font.Color = Color.Red;
worksheet.Cells["A1"].FillColor = Color.Yellow;

// 设置单元格区域的格式。
DevExpress.Spreadsheet.CellRange range = worksheet.Range["C3:D4"];
DevExpress.Spreadsheet.Formatting rangeFormatting = range.BeginUpdateFormatting();
rangeFormatting.Font.Color = Color.Blue;
rangeFormatting.Fill.BackgroundColor = Color.LightBlue;
rangeFormatting.Fill.PatternType = DevExpress.Spreadsheet.PatternType.LightHorizontal;
rangeFormatting.Fill.PatternColor = Color.Violet;
range.EndUpdateFormatting(rangeFormatting);

下图显示了彩色单元格(工作簿在 Microsoft® Excel® 中打开)。