条件格式设置
条件格式设置是Microsoft Excel的高级功能,允许您对单元格或一系列单元格应用格式,并根据单元格的值或公式的值进行更改。例如,只有当单元格的值大于500时,单元格才会显示为粗体。当单元格的值满足条件时,将应用指定格式到单元格。如果单元格的值不满足条件,则使用默认格式。在Microsoft Excel中,选择格式,然后条件格式来打开条件格式设置对话框。
Microsoft Excel中的条件格式设置
Aspose.Cells支持在运行时向单元格应用条件格式。本文介绍了如何实现这一点。
应用条件格式
Aspose.Cells支持两种方式的条件格式:
使用设计者电子表格
开发人员可以在Microsoft Excel中创建包含条件格式设置的设计者电子表格,然后使用Aspose.Cells打开该电子表格。Aspose.Cells加载和保存设计者电子表格,保留任何条件格式设置。欲了解更多有关设计者电子表格的信息,请阅读设计者电子表格是什么
在运行时应用条件格式
Aspose.Cells支持在运行时应用条件格式。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(ConditionalFormattingatRuntime.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
ConditionalFormattingCollection cfs = sheet.getConditionalFormattings(); | |
// The first method:adds an empty conditional formatting | |
int index = cfs.add(); | |
FormatConditionCollection fcs = cfs.get(index); | |
// Sets the conditional format range. | |
CellArea ca1 = new CellArea(); | |
ca1.StartRow = 0; | |
ca1.StartColumn = 0; | |
ca1.EndRow = 0; | |
ca1.EndColumn = 0; | |
CellArea ca2 = new CellArea(); | |
ca2.StartRow = 0; | |
ca2.StartColumn = 0; | |
ca2.EndRow = 0; | |
ca2.EndColumn = 0; | |
CellArea ca3 = new CellArea(); | |
ca3.StartRow = 0; | |
ca3.StartColumn = 0; | |
ca3.EndRow = 0; | |
ca3.EndColumn = 0; | |
fcs.addArea(ca1); | |
fcs.addArea(ca2); | |
fcs.addArea(ca3); | |
// Sets condition formulas. | |
int conditionIndex = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "=A2", "100"); | |
FormatCondition fc = fcs.get(conditionIndex); | |
int conditionIndex2 = fcs.addCondition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100"); | |
// Saving the Excel file | |
workbook.save(dataDir + "CFAtRuntime_out.xls"); |
设置字体
在Microsoft Excel中设置字体
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(SettingFontStyle.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
FormatCondition fc = null; | |
Style style = fc.getStyle(); | |
Font font = style.getFont(); | |
font.setItalic(true); | |
font.setBold(true); | |
font.setStrikeout(true); | |
font.setUnderline(FontUnderlineType.DOUBLE); | |
font.setColor(Color.getBlack()); | |
fc.setStyle(style); | |
// Saving the Excel file | |
workbook.save(dataDir + "SFontStyle_out.xls"); |
设置边框
在Microsoft Excel中设置边框
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(SetBorder.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Adds an empty conditional formatting | |
int index = sheet.getConditionalFormattings().add(); | |
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(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.CELL_VALUE, OperatorType.BETWEEN, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs.get(conditionIndex); | |
Style style = fc.getStyle(); | |
style.setBorder(BorderType.LEFT_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255)); | |
style.setBorder(BorderType.TOP_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255)); | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.DASHED, Color.fromArgb(0, 255, 255)); | |
style.setBorder(BorderType.RIGHT_BORDER, CellBorderType.DASHED, Color.fromArgb(255, 255, 0)); | |
fc.setStyle(style); |
设置填充
在Microsoft Excel中设置单元格填充
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// Path to source file | |
String dataDir = Utils.getSharedDataDir(SetPattern.class) + "data/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
Worksheet sheet = workbook.getWorksheets().get(0); | |
// Adds an empty conditional formatting | |
int index = sheet.getConditionalFormattings().add(); | |
FormatConditionCollection fcs = sheet.getConditionalFormattings().get(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.CELL_VALUE, OperatorType.BETWEEN, "50", "100"); | |
// Sets the background color. | |
FormatCondition fc = fcs.get(conditionIndex); | |
Style style = fc.getStyle(); | |
style.setPattern(BackgroundType.REVERSE_DIAGONAL_STRIPE); | |
style.setForegroundColor(Color.fromArgb(255, 255, 0)); | |
style.setBackgroundColor(Color.fromArgb(0, 255, 255)); | |
fc.setStyle(style); |