Formulas and functions are fundamental features of Microsoft Excel that allow users to perform a variety of mathematical, statistical, and logical operations on data. Formulas are expressions that can be entered into cells to automate calculations, usually consisting of cell references, constants, and operators that specify the calculation to be performed. Functions, on the other hand, are pre-built formulas that perform specific tasks, such as calculating the sum, average, maximum, or minimum value of a range of cells. Both formulas and functions are essential tools for anyone working with data in Excel. Whether you are analyzing financial data, experimental data, or any other dataset, using formulas and functions can help you quickly and accurately perform calculations on your data and gain insights from it. In this article, you will learn how to insert or Read formulas and functions in an Excel file in C++ using Spire.XLS for C++.
Install Spire.XLS for C++
There are two ways to integrate Spire.XLS for C++ into your application. One way is to install it through NuGet, and the other way is to download the package from our website and copy the libraries into your program. Installation via NuGet is simpler and more recommended. You can find more details by visiting the following link.
Integrate Spire.XLS for C++ in a C++ Application
Insert Formulas and Functions into Excel in C++
The Worksheet->GetRange(int row, int column)->SetFormula(LPCWSTR_S value) method in Spire.XLS for C++ is used to add formulas or functions to specific cells in an Excel worksheet. The main steps are as follows:
- Initialize an instance of the Workbook class.
- Get a specific worksheet by its index using the Workbook->GetWorksheets()->Get(int index) method.
- Add some text and numeric data to specific cells of the worksheet using the Worksheet->GetRange(int row, int column)->SetText(LPCWSTR_S value) and Worksheet->GetRange(int row, int column)->SetNumberValue(double value) methods.
- Add text and formulas to specific cells of the worksheet using the Worksheet->GetRange(int row, int column)->SetText(LPCWSTR_S value) and the Worksheet->GetRange(int row, int column)->SetFormula(LPCWSTR_S value) methods.
- Add text and functions to specific cells of the worksheet using the Worksheet->GetRange(int row, int column)->SetText(LPCWSTR_S value) and the Worksheet->GetRange(int row, int column)->SetFormula(LPCWSTR_S value) methods.
- Save the result file using Workbook->SaveToFile(LPCWSTR_S fileName, ExcelVersion version) method.
- C++
#include "Spire.Xls.o.h";
using namespace Spire::Xls;
using namespace std;
int main()
{
//Initialize an instance of the Workbook class
Workbook* workbook = new Workbook();
//Get the first worksheet
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//Declare two variables: currentRow, currentFormula
int currentRow = 1;
wstring currentFormula = L"";
//Add text to the worksheet and set cell style
sheet->GetRange(currentRow, 1)->SetText(L"Test Data:");
sheet->GetRange(currentRow, 1)->GetStyle()->GetFont()->SetIsBold(true);
sheet->GetRange(currentRow, 1)->GetStyle()->SetFillPattern(ExcelPatternType::Solid);
sheet->GetRange(currentRow, 1)->GetStyle()->SetKnownColor(ExcelColors::LightGreen1);
sheet->GetRange(currentRow, 1)->GetStyle()->GetBorders()->Get(BordersLineType::EdgeBottom)->SetLineStyle(LineStyleType::Medium);
//Add some numeric data to the worksheet
sheet->GetRange(++currentRow, 1)->SetNumberValue(7.3);
sheet->GetRange(currentRow, 2)->SetNumberValue(5);
sheet->GetRange(currentRow, 3)->SetNumberValue(8.2);
sheet->GetRange(currentRow, 4)->SetNumberValue(4);
sheet->GetRange(currentRow, 5)->SetNumberValue(3);
sheet->GetRange(currentRow, 6)->SetNumberValue(11.3);
currentRow++;
//Add text to the worksheet and set cell style
sheet->GetRange(++currentRow, 1)->SetText(L"Formulas");
sheet->GetRange(currentRow, 2)->SetText(L"Results");
sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->GetFont()->SetIsBold(true);
sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->SetKnownColor(ExcelColors::LightGreen1);
sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->SetFillPattern(ExcelPatternType::Solid);
sheet->GetRange(currentRow, 1, currentRow, 2)->GetStyle()->GetBorders()->Get(BordersLineType::EdgeBottom)->SetLineStyle(LineStyleType::Medium);
//Add text and formulas to the worksheet
currentFormula = (L"=\"Hello\"");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
currentFormula = (L"=300");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
currentFormula = (L"=3389.639421");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
currentFormula = (L"=false");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
currentFormula = (L"=1+2+3+4+5-6-7+8-9");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
currentFormula = (L"=33*3/4-2+10");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
currentFormula = (L"=Sheet1!$B$2");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
//Add text and Functions to the worksheet
//AVERAGE
currentFormula = (L"=AVERAGE(Sheet1!$D$2:F$2)");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
//COUNT
currentFormula = (L"=COUNT(3,5,8,10,2,34)");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
//NOW
currentFormula = (L"=NOW()");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow, 2)->SetFormula(currentFormula.c_str());
sheet->GetRange(currentRow, 2)->GetStyle()->SetNumberFormat(L"yyyy-MM-DD");
//SECOND
currentFormula = (L"=SECOND(0.503)");
sheet->GetRange(++currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//MINUTE
currentFormula = (L"=MINUTE(0.78125)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//MONTH
currentFormula = (L"=MONTH(9)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//DAY
currentFormula = (L"=DAY(10)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//TIME
currentFormula = (L"=TIME(4,5,7)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//DATE
currentFormula = (L"=DATE(6,4,2)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//RAND
currentFormula = (L"=RAND()");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//HOUR
currentFormula = (L"=HOUR(0.5)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//MOD
currentFormula = (L"=MOD(5,3)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//WEEKDAY
currentFormula = (L"=WEEKDAY(3)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//YEAR
currentFormula = (L"=YEAR(23)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//NOT
currentFormula = (L"=NOT(true)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//OR
currentFormula = (L"=OR(true)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//AND
currentFormula = (L"=AND(TRUE)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//VALUE
currentFormula = (L"=VALUE(30)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//LEN
currentFormula = (L"=LEN(\"world\")");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//MID
currentFormula = (L"=MID(\"world\",4,2)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//ROUND
currentFormula = (L"=ROUND(7,3)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//SIGN
currentFormula = (L"=SIGN(4)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//INT
currentFormula = (L"=INT(200)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//ABS
currentFormula = (L"=ABS(-1.21)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//LN
currentFormula = (L"=LN(15)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//EXP
currentFormula = (L"=EXP(20)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//SQRT
currentFormula = (L"=SQRT(40)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//PI
currentFormula = (L"=PI()");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//COS
currentFormula = (L"=COS(9)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//SIN
currentFormula = (L"=SIN(45)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//MAX
currentFormula = (L"=MAX(10,30)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//MIN
currentFormula = (L"=MIN(5,7)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//AVERAGE
currentFormula = (L"=AVERAGE(12,45)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//SUM
currentFormula = (L"=SUM(18,29)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//IF
currentFormula = (L"=IF(4,2,2)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//SUBTOTAL
currentFormula = (L"=SUBTOTAL(3,Sheet1!A2:F2)");
sheet->GetRange(currentRow, 1)->SetText((L"'" + currentFormula).c_str());
sheet->GetRange(currentRow++, 2)->SetFormula(currentFormula.c_str());
//Set width of the 1st, 2nd and 3rd columns
sheet->SetColumnWidth(1, 32);
sheet->SetColumnWidth(2, 16);
sheet->SetColumnWidth(3, 16);
//Create a cell style
CellStyle* style = workbook->GetStyles()->Add(L"Style");
//Set the horizontal alignment as left
style->SetHorizontalAlignment(HorizontalAlignType::Left);
//Apply the style to the worksheet
sheet->ApplyStyle(style);
//Save the result file
workbook->SaveToFile(L"InsertFormulasAndFunctions.xlsx", ExcelVersion::Version2016);
workbook->Dispose();
delete workbook;
}

Read Formulas and Functions in Excel in C++
To read formulas and functions in an Excel worksheet, you need to iterate through all the cells in the worksheet, after that, find the cells containing formulas or functions using the Cell->GetHasFormula() method, then get the formulas or functions of the cells using the CellRange->GetFormula() method. The detailed steps are as follows:
- Initialize an instance of the Workbook class.
- Load an Excel file using the Workbook->LoadFromFile() method.
- Get a specific worksheet by its index using the Workbook->GetWorksheets()->Get(int index) method.
- Access the used range of the worksheet using the Worksheet->GetAllocatedRange() method.
- Declare a wstring variable.
- Iterate through all the cells in the used range.
- Find the cells containing formulas/functions using the Cell->GetHasFormula() method.
- Get the names and the formulas/functions of the cells using the CellRange->GetRangeAddressLocal() and CellRange->GetFormula() methods.
- Append the cell names and formulas/functions to the wstring variable.
- Write the content of the wstring variable into a .txt file.
- C++
#include "Spire.Xls.o.h";
#include <locale>
#include <codecvt>
using namespace Spire::Xls;
using namespace std;
int main()
{
//Initialize an instance of the Workbook class
Workbook* workbook = new Workbook();
//Load an Excel file
workbook->LoadFromFile(L"InsertFormulasAndFunctions.xlsx");
//Get the first worksheet
Worksheet* sheet = workbook->GetWorksheets()->Get(0);
//Access the used range of the worksheet
CellRange* usedRange = sheet->GetAllocatedRange();
//Declare a wstring variable
wstring buffer = L"";
//Loop through all the cells in the used range
for(int i = 0; i < usedRange->GetCells()->GetCount(); i++)
{
CellRange* cell = usedRange->GetCells()->GetItem(i);
//Detect if the current cell has formula/function
if (cell->GetHasFormula())
{
//Get the cell name
wstring cellName = cell->GetRangeAddressLocal();
//Get the formula/function
wstring formula = cell->GetFormula();
//Append the cell name and formula/function to the wstring variable
buffer += ((cellName + L" has a formula: " + formula + L"\n").c_str());
}
}
//Write the content of the wstring variable into a .txt file
wofstream write(L"ReadFormulasAndFunctions.txt");
auto LocUtf8 = locale(locale(""), new std::codecvt_utf8<wchar_t>);
write.imbue(LocUtf8);
write << buffer;
write.close();
workbook->Dispose();
delete workbook;
}

Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
