Apply Conditional Formatting in Worksheets with C++
This article is designed to provide a detailed understanding of how to add conditional formatting to a range of cells in a worksheet.
Conditional formatting is an advanced feature in Microsoft Excel that allows you to apply formats to a range of cells, and have that formatting change depending on the value of the cell or the value of a formula. For example, the background of a cell may be red to highlight a negative value, or the text color might be green for a positive value. When the value of the cell meets the format condition, the format is applied. If the value of the cell does not meet the format condition, the cell’s default formatting is used.
It’s possible to apply conditional formatting with Microsoft Office Automation but that has its drawbacks. There are several reasons and issues involved: for example, security, stability, scalability and speed. The main reason for finding another solution is that Microsoft themselves strongly recommends against Office Automation for software solutions.
This article shows how to create a console application, add conditional formatting on cells with a few simplest lines of code using the Aspose.Cells API.
Using Aspose.Cells to Apply Conditional Formatting Based on Cell Value
- Download and Install Aspose.Cells.
- Download Aspose.Cells for C++.
- Install it on your development computer. All Aspose components, when installed, work in evaluation mode. The evaluation mode has no time limit and it only injects watermarks into produced documents.
- Create a project. Start your C++ development environment and create a new console application.
- Add references. Add a reference to Aspose.Cells to your project, for example add a reference to ….\Program Files\Aspose\Aspose.Cells\Bin\Net1.0\Aspose.Cells.dll
- Apply conditional formatting based on cell value. Below is the code used to accomplish the task. It applies conditional formatting on a cell.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Instantiating a Workbook object
Workbook workbook;
// Get the first worksheet
Worksheet sheet = workbook.GetWorksheets().Get(0);
// Adds an empty conditional formatting
int index = sheet.GetConditionalFormattings().Add();
// Get the FormatConditionCollection
FormatConditionCollection fcs = sheet.GetConditionalFormattings().Get(index);
// Sets the conditional format range
CellArea ca = CellArea::CreateCellArea(0, 0, 0, 0);
// Add the cell area to the format condition collection
fcs.AddArea(ca);
// Adds condition
int conditionIndex = fcs.AddCondition(FormatConditionType::CellValue, OperatorType::Between, u"50", u"100");
// Get the format condition
FormatCondition fc = fcs.Get(conditionIndex);
// Sets the background color
fc.GetStyle().SetBackgroundColor(Color::Red());
// Saving the Excel file
workbook.Save(outDir + u"output.out.xls", SaveFormat::Auto);
std::cout << "Conditional formatting applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
When the above code is executed, conditional formatting is applied to cell “A1” in the first worksheet of the output file (output.xls). The conditional formatting applied to A1 depends on the cell value. If the cell value of A1 is between 50 and 100 the background color is red due to the conditional formatting applied.
Using Aspose.Cells to Apply Conditional Formatting Based on Formula
- Applying conditional formatting depending on formula (Code Snippet) Below is the code to accomplish the task. It applies conditional formatting on B3.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
// Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create workbook
Workbook workbook;
// Get the first worksheet
Worksheet sheet = workbook.GetWorksheets().Get(0);
// Adds an empty conditional formatting
int index = sheet.GetConditionalFormattings().Add();
// Get the conditional formatting collection
FormatConditionCollection fcs = sheet.GetConditionalFormattings().Get(index);
// Sets the conditional format range
CellArea ca = CellArea::CreateCellArea(2, 1, 2, 1);
// Add the area to the conditional formatting
fcs.AddArea(ca);
// Adds condition
int conditionIndex = fcs.AddCondition(FormatConditionType::Expression);
// Get the format condition
FormatCondition fc = fcs.Get(conditionIndex);
// Set the formula for the condition
fc.SetFormula1(u"=IF(SUM(B1:B2)>100,TRUE,FALSE)");
// Set the background color
Style style = fc.GetStyle();
style.SetBackgroundColor(Color::Red());
fc.SetStyle(style);
// Set the formula for cell B3
sheet.GetCells().Get(u"B3").SetFormula(u"=SUM(B1:B2)");
// Set the value for cell C4
sheet.GetCells().Get(u"C4").PutValue(u"If Sum of B1:B2 is greater than 100, B3 will have RED background");
// Save the Excel file
workbook.Save(outDir + u"output.out.xls", SaveFormat::Auto);
std::cout << "Conditional formatting applied successfully!" << std::endl;
Aspose::Cells::Cleanup();
}
When the above code is executed, conditional formatting is applied to cell “B3” in the first worksheet of the output file (output.xls). The conditional formatting applied depends on the formula which calculates the value of “B3” as the sum of B1 & B2.