设置 Excel 和 ODS 文件的条件格式。
Contents
[
Hide
]
介绍
条件格式是一项高级 Microsoft Excel 功能,允许您将格式应用于单元格或单元格区域,并根据单元格的值或公式的值更改格式。例如,您可以让单元格仅在单元格的值大于 500 时显示为粗体。当单元格的值满足条件时,指定的格式将应用于单元格。如果单元格的值不满足格式条件,则使用单元格的默认格式。在 Microsoft Excel 中,选择格式, 然后条件格式打开条件格式对话框。
Aspose.Cells 支持在运行时对单元格应用条件格式。这篇文章解释了如何。它还解释了如何计算 Excel 用于色标条件格式的颜色。
应用条件格式
Aspose.Cells 通过几种方式支持条件格式:
- 使用设计器电子表格
- 使用复制方法。
- 在运行时创建条件格式。
使用设计器电子表格
开发人员可以在 Microsoft Excel 中创建包含条件格式的设计器电子表格,然后使用 Aspose.Cells 打开该电子表格。Aspose.Cells 加载并保存设计器电子表格,同时保留任何条件格式设置。
使用复制方法
Aspose.Cells 允许开发人员通过调用范围.复制()方法。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "Book1.xlsx", FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Copying conditional format settings from cell "A1" to cell "B1" | |
//worksheet.CopyConditionalFormatting(0, 0, 0, 1); | |
int TotalRowCount = 0; | |
for (int i = 0; i < workbook.Worksheets.Count; i++) | |
{ | |
Worksheet sourceSheet = workbook.Worksheets[i]; | |
Range sourceRange = sourceSheet.Cells.MaxDisplayRange; | |
Range destRange = worksheet.Cells.CreateRange(sourceRange.FirstRow + TotalRowCount, sourceRange.FirstColumn, | |
sourceRange.RowCount, sourceRange.ColumnCount); | |
destRange.Copy(sourceRange); | |
TotalRowCount = sourceRange.RowCount + TotalRowCount; | |
} | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
在运行时应用条件格式
Aspose.Cells 允许您在运行时添加和删除条件格式。下面的代码示例显示了如何设置条件格式:
- 实例化工作簿。
- 添加一个空的条件格式。
- 设置格式应适用的范围。
- 定义格式化条件。
- 保存文件。
在这个示例之后是许多其他较小的示例,这些示例展示了如何应用字体设置、边框设置和图案。
Microsoft Excel 2007 添加了 Aspose.Cells 也支持的更高级的条件格式。此处的示例说明如何使用简单格式,Microsoft Excel 2007 示例说明如何应用更高级的条件格式。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
string filePath = dataDir + "Book1.xlsx"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Adds an empty conditional formatting | |
int index = sheet.ConditionalFormattings.Add(); | |
FormatConditionCollection fcs = sheet.ConditionalFormattings[index]; | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 0; | |
ca.StartColumn = 0; | |
ca.EndColumn = 0; | |
fcs.AddArea(ca); | |
ca = new CellArea(); | |
ca.StartRow = 1; | |
ca.EndRow = 1; | |
ca.StartColumn = 1; | |
ca.EndColumn = 1; | |
fcs.AddArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100"); | |
// Adds condition. | |
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs[conditionIndex]; | |
fc.Style.BackgroundColor = Color.Red; | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
设置字体
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 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 | |
Aspose.Cells.Cell cell = worksheet.Cells["A1"]; | |
// Adding some value to the "A1" cell | |
cell.PutValue("Hello Aspose!"); | |
// Obtaining the style of the cell | |
Style style = cell.GetStyle(); | |
// Setting the font weight to bold | |
style.Font.IsBold = true; | |
// Applying the style to the cell | |
cell.SetStyle(style); | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls", SaveFormat.Excel97To2003); |
您只能更改字体样式、文本颜色、下划线样式和删除线样式。
设置边框
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public static void Run() | |
{ | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Adds an empty conditional formatting | |
int index = sheet.ConditionalFormattings.Add(); | |
FormatConditionCollection fcs = sheet.ConditionalFormattings[index]; | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 5; | |
ca.StartColumn = 0; | |
ca.EndColumn = 3; | |
fcs.AddArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs[conditionIndex]; | |
fc.Style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Dashed; | |
fc.Style.Borders[BorderType.LeftBorder].Color = Color.FromArgb(0, 255, 255); | |
fc.Style.Borders[BorderType.RightBorder].Color = Color.FromArgb(0, 255, 255); | |
fc.Style.Borders[BorderType.TopBorder].Color = Color.FromArgb(0, 255, 255); | |
fc.Style.Borders[BorderType.BottomBorder].Color = Color.FromArgb(255, 255, 0); | |
workbook.Save(dataDir + "output.xlsx"); | |
} |
您只能对轮廓边框使用细线样式。不允许使用对角线。
设置模式
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Adds an empty conditional formatting | |
int index = sheet.ConditionalFormattings.Add(); | |
FormatConditionCollection fcs = sheet.ConditionalFormattings[index]; | |
// Sets the conditional format range. | |
CellArea ca = new CellArea(); | |
ca.StartRow = 0; | |
ca.EndRow = 5; | |
ca.StartColumn = 0; | |
ca.EndColumn = 3; | |
fcs.AddArea(ca); | |
// Adds condition. | |
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100"); | |
FormatCondition fc = fcs[conditionIndex]; | |
fc.Style.Pattern = BackgroundType.ReverseDiagonalStripe; | |
fc.Style.ForegroundColor = Color.FromArgb(255, 255, 0); | |
fc.Style.BackgroundColor = Color.FromArgb(0, 255, 255); | |
workbook.Save(dataDir + "output.xlsx"); |