Documentation Buy Contact Blog
Formula Example

It's super easy to add or read formulas from your Excel spreadsheets. They are just strings the same as you would type into Excel itself.

This is what your final result will look like
This is what your final result will look like.

In the code below you can see that the cells in the B column are just simple strings, while the C column has formulas set and are being calculated.

Select your language below to view the source. You can download Number Duck here, along with source code to all the examples.

C++C#
C++
printf("Formula Example\n");
printf("Create a spreadsheet with formulas!\n\n");

Workbook* pWorkbook = new Workbook();
Worksheet* pWorksheet = pWorkbook->GetWorksheetByIndex(0);
	
for (int i = 0; i < 5; i++)
{
	Cell* pCell = pWorksheet->GetCell(0, i);
	pCell->SetFloat(i * 2.34f);
}

pWorksheet->GetCell(1,0)->SetString("=SUM(A1:A5)");
pWorksheet->GetCell(1,1)->SetString("=AVERAGE(A1:A5)");

pWorksheet->GetCell(2,0)->SetFormula("=SUM(A1:A5)");
pWorksheet->GetCell(2,1)->SetFormula("=AVERAGE(A1:A5)");
	
pWorkbook->Save("FormulaExample.xls", Workbook::FILE_TYPE_XLS);

delete pWorkbook;


Workbook* pWorkbookIn = new Workbook();
if (pWorkbookIn->Load("FormulaExample.xls"))
{
	Worksheet* pWorksheetIn = pWorkbookIn->GetWorksheetByIndex(0);
	Cell* pCellIn = pWorksheetIn->GetCell(2,1);
	printf("Formula: %s\n", pCellIn->GetFormula());
}
delete pWorkbookIn;