设置Excel和ODS文件的条件格式。
介绍
条件格式是Microsoft Excel的高级功能,允许您对单元格或一系列单元格应用格式,并根据单元格的数值或公式的值更改格式。例如,只有当单元格的值大于500时,该单元格才会显示为粗体。当单元格的值满足条件时,会将指定的格式应用到单元格。如果单元格的值不符合格式条件,则使用单元格的默认格式。在Microsoft Excel中,选择格式,然后选择条件格式以打开条件格式对话框。
Aspose.Cells支持在运行时将条件格式应用于单元格。本文解释了如何实现这一点。它还解释了如何计算Excel用于颜色标度条件格式的颜色。
应用条件格式
Aspose.Cells支持多种方式的条件格式:
- 使用设计师电子表格
- 使用复制方法。
- 在运行时创建条件格式。
使用设计者电子表格
开发人员可以在Microsoft Excel中创建一个包含条件格式的设计师电子表格,然后用Aspose.Cells打开该电子表格。Aspose.Cells加载并保存设计师电子表格,并保留任何条件格式设置。
使用复制方法
Aspose.Cells允许开发人员通过调用Range.Copy()方法将一个单元格的条件格式设置复制到工作表中的另一个单元格。
// 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的示例则展示了如何应用更高级的条件格式设置。
// 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"); |
设置字体
// 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); |
设置边框
// 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"); | |
} |
设置填充
// 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"); |