Introduction

Spreadsheet management is the part of Aspose.Cells FOSS for C++ that most programs touch first: the Workbook that owns the file, the Worksheet objects inside it, and the Cells collection that holds values, formulas, and formatting. This post walks through that surface in more depth than a general introduction — how workbooks and worksheets are created and navigated, how cell values and formulas are read and written, how Style, Font, Color, and Borders combine to format output, and how PageSetup, Hyperlink, DefinedName, and Validation add structure around the raw grid of cells.

Every class covered here is part of the native C++ API declared under aspose/cells_foss/ headers. There is no COM interop and no dependency on Microsoft Excel being installed — the library reads and writes the .xlsx package format directly, which is also why the current release supports .xlsx only, on both the load and save sides.

The examples below build on each other: a workbook is created, worksheets and cells are populated, styling is applied, and the file is saved and reloaded to confirm the round trip. All code is adapted from the library’s own compatibility, golden, and unit test suites, so it reflects operations that are exercised against real .xlsx output.


Key Features

Workbook and Worksheet Management

A Workbook starts with one worksheet in its WorksheetCollection. Worksheets are added with Add(), looked up by index or by name, and renamed with SetName(). WorksheetCollection also tracks which sheet is active:

#include "aspose/cells_foss/Workbook.h"
#include "aspose/cells_foss/Worksheet.h"

using namespace Aspose::Cells_FOSS;

Workbook workbook;
Worksheet& sheet = workbook.GetWorksheets()[0];
sheet.SetName("Data");

int summaryIndex = workbook.GetWorksheets().Add("Summary");
workbook.GetWorksheets().SetActiveSheetIndex(0);

int sheetCount = workbook.GetWorksheets().GetCount(); // 2

Saving and reloading go through the same Workbook type. Workbook::Save() accepts a file path, and a Workbook can be constructed directly from a path or from an in-memory byte buffer, which round-trips cell values and formulas:

workbook.Save("report.xlsx");

Workbook loaded("report.xlsx");
Worksheet& reloadedSheet = loaded.GetWorksheets()["Data"];

Cell Values and Formulas

Cells, reached through Worksheet::GetCells(), indexes cells by A1-style reference. Cell::PutValue() accepts strings, integers, doubles, and booleans, SetFormula() stores an Excel formula string, and GetValue() returns a CellValue with typed accessors (AsString(), AsInteger(), AsDouble(), AsBool(), AsDateTime()):

Cells& cells = sheet.GetCells();
cells["A1"].PutValue("Category");
cells["B1"].PutValue("Amount");
cells["A2"].PutValue("Travel");
cells["B2"].PutValue(482.50);
cells["A3"].PutValue("Supplies");
cells["B3"].PutValue(129.99);
cells["B4"].SetFormula("=SUM(B2:B3)");

CellValue amount = cells["B2"].GetValue();
if (amount.IsDouble()) {
    double value = amount.AsDouble();
}

std::string formula = cells["B4"].GetFormula();

Cell::GetType() reports the stored CellValueType (Blank, String, Number, Boolean, DateTime, or Formula), and GetDisplayStringValue() returns the value formatted the way it would appear in Excel, honoring whatever number format is applied through Style.

Styling with Style, Font, Color, and Borders

Cell::GetStyle() returns a Style object that can be modified and reapplied with SetStyle(). Style exposes a Font, a Borders set, fill pattern and color, alignment, and number format:

#include "aspose/cells_foss/Style.h"
#include "aspose/cells_foss/Color.h"

Cell headerCell = sheet.GetCells()["A1"];
Style style = headerCell.GetStyle();

Font font = style.GetFont();
font.SetBold(true);
font.SetColor(Color::FromArgb(255, 255, 255, 255));
style.SetFont(font);

style.SetPattern(FillPattern::Solid);
style.SetForegroundColor(Color::FromArgb(255, 34, 120, 212));
style.SetHorizontalAlignment(HorizontalAlignmentType::Center);

Borders borders = style.GetBorders();
Border bottomBorder = borders.GetBottom();
bottomBorder.SetLineStyle(BorderStyleType::Thin);
borders.SetBottom(bottomBorder);
style.SetBorders(borders);

style.SetNumberFormat("#,##0.00");
headerCell.SetStyle(style);

Style is a value type: reading it back with GetStyle() reflects whatever was last set with SetStyle(), which makes it straightforward to build one style and apply it across a header row or a range of data cells.

Page Setup and Print Configuration

Worksheet::GetPageSetup() returns a PageSetup object covering paper size, orientation, margins, scaling, print titles, and page breaks:

#include "aspose/cells_foss/PageSetup.h"

PageSetup& pageSetup = sheet.GetPageSetup();
pageSetup.SetOrientation(PageOrientationType::Landscape);
pageSetup.SetPaperSize(PaperSizeType::PaperA4);
pageSetup.SetFitToPagesWide(1);
pageSetup.SetFitToPagesTall(0);
pageSetup.SetPrintArea("$A$1:$D$20");
pageSetup.SetPrintTitleRows("$1:$1");
pageSetup.SetLeftMargin(0.5);
pageSetup.SetCenterFooter("Page &P of &N");
pageSetup.AddHorizontalPageBreak(20);

Margins are available in both raw units (GetLeftMargin()) and inches (GetLeftMarginInch()), and GetHorizontalPageBreaks() / GetVerticalPageBreaks() return the page break positions currently set on the sheet.

Worksheet::GetHyperlinks() returns a HyperlinkCollection. Add() takes a target cell or range plus an address and returns the new hyperlink’s index, which is used to fetch the Hyperlink object for further changes:

#include "aspose/cells_foss/Hyperlink.h"

sheet.GetCells()["A1"].PutValue("Documentation");
int linkIndex = sheet.GetHyperlinks().Add("A1", 1, 1, "https://example.com/docs");
Hyperlink link = sheet.GetHyperlinks()[linkIndex];
link.SetTextToDisplay("Documentation");
link.SetScreenTip("Open the docs site");

Workbook::GetDefinedNames() manages named ranges the same way — Add() returns an index into the DefinedNameCollection:

#include "aspose/cells_foss/DefinedName.h"

int nameIndex = workbook.GetDefinedNames().Add("Total", "=Data!$B$2:$B$3");
DefinedName total = workbook.GetDefinedNames()[nameIndex];
total.SetComment("Sum of tracked expenses");

Worksheet::GetValidations() follows the same collection pattern for data validation rules, scoped to a CellArea:

#include "aspose/cells_foss/Validation.h"
#include "aspose/cells_foss/CellArea.h"

int ruleIndex = sheet.GetValidations().Add(CellArea::CreateCellArea("B2", "B3"));
Validation amountRule = sheet.GetValidations()[ruleIndex];
amountRule.SetType(ValidationType::Decimal);
amountRule.SetOperator(OperatorType::GreaterThan);
amountRule.SetFormula1("0");
amountRule.SetErrorTitle("Invalid Amount");
amountRule.SetErrorMessage("Enter a positive number");

Document Properties and Workbook Settings

Workbook::GetDocumentProperties() exposes the core and extended OOXML metadata fields (title, author, company, manager, and similar), and Workbook::GetSettings() controls workbook-level behavior such as the date system:

#include "aspose/cells_foss/DocumentProperties.h"

workbook.GetDocumentProperties().SetCompany("Example Corp");
workbook.GetDocumentProperties().SetManager("Finance Team");

workbook.GetSettings().SetDate1904(false);

These values round-trip through save and reload the same way cell content does, which is useful for reports that need consistent metadata across generated files.


Quick Start

Aspose.Cells FOSS for C++ is not published to a package registry; it is built from source with CMake. Clone the repository and either add it as a subdirectory or pull it in with FetchContent:

git clone https://github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-Cpp.git
cmake_minimum_required(VERSION 3.15)
project(MyProject CXX)
set(CMAKE_CXX_STANDARD 17)

add_subdirectory(path/to/Aspose.Cells-FOSS-for-Cpp)

add_executable(MyApp main.cpp)
target_link_libraries(MyApp PRIVATE Aspose.Cells.Foss.Cpp)

A minimal spreadsheet-management example — create, style, save, and reload:

#include "aspose/cells_foss/Workbook.h"
#include "aspose/cells_foss/Worksheet.h"
#include "aspose/cells_foss/Cell.h"

using namespace Aspose::Cells_FOSS;

int main() {
    Workbook workbook;
    Worksheet& sheet = workbook.GetWorksheets()[0];
    sheet.SetName("Expenses");

    Cells& cells = sheet.GetCells();
    cells["A1"].PutValue("Category");
    cells["B1"].PutValue("Amount");
    cells["A2"].PutValue("Travel");
    cells["B2"].PutValue(482.50);
    cells["A3"].PutValue("Supplies");
    cells["B3"].PutValue(129.99);
    cells["B4"].SetFormula("=SUM(B2:B3)");

    workbook.Save("expenses.xlsx");

    Workbook loaded("expenses.xlsx");
    Cells& loadedCells = loaded.GetWorksheets()["Expenses"].GetCells();
    double total = loadedCells["B4"].GetValue().AsDouble();

    return 0;
}

Supported Formats

FormatExtensionReadWrite
Xlsx.xlsx

LoadFormat and SaveFormat both expose a single Xlsx value in this release; the Workbook(path) and Workbook(bytes) constructors load .xlsx input, and Workbook::Save() writes .xlsx output to a path, a stream, or an in-memory buffer.


Open Source & Licensing

Aspose.Cells FOSS for C++ is released under the MIT license. Source code is available on GitHub, and the library can be used, modified, and redistributed in commercial and open-source projects without runtime fees.


Getting Started