How to Create and Populate a Spreadsheet Data Model in C++

How to Create and Populate a Spreadsheet Data Model in C++

This guide shows how to create a workbook, populate cells with values and formulas, apply styles, and save as a spreadsheet file using the Aspose.Cells FOSS for C++ library. The core classes involved are Workbook, Worksheet, Cell, and Style.

Step-by-Step Guide

Step 1: Set Up the Build

Add the Cells FOSS library to your CMake project and link the Aspose.Cells.Foss.Cpp target:

cmake_minimum_required(VERSION 3.15)
project(CreateSpreadsheet CXX)
set(CMAKE_CXX_STANDARD 17)

add_subdirectory(path/to/Aspose.Cells-FOSS-for-Cpp)
add_executable(CreateSpreadsheet main.cpp)
target_link_libraries(CreateSpreadsheet PRIVATE Aspose.Cells.Foss.Cpp)

Step 2: Include Headers and Set Namespace

Include the per-class headers from aspose/cells_foss/ for each class you use (Workbook.h, Worksheet.h, Cell.h, Style.h, Font.h, Color.h, CellArea.h), then open the Aspose::Cells_FOSS namespace:

using namespace Aspose::Cells_FOSS;

Step 3: Create a Workbook and Name the Sheet

Instantiate Workbook, retrieve the first worksheet via GetWorksheets()[0], and set its display name:

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

Step 4: Add Cell Values

Call GetCells()["A1"].PutValue() to write text strings, numeric values, and booleans into individual cells by address:

sheet.GetCells()["A1"].PutValue("Product");
sheet.GetCells()["B1"].PutValue("Price");
sheet.GetCells()["A2"].PutValue("Widget");
sheet.GetCells()["B2"].PutValue(9.99);
sheet.GetCells()["A3"].PutValue("Gadget");
sheet.GetCells()["B3"].PutValue(24.99);

Step 5: Set a Formula

Call SetFormula() on a cell to store an Excel-compatible formula string; the formula is evaluated by Excel when the file is opened:

sheet.GetCells()["B4"].SetFormula("=SUM(B2:B3)");

Step 6: Apply Cell Styling

Get the style with Cell::GetStyle(), modify it, and apply with Cell::SetStyle(style):

Style headerStyle = sheet.GetCells()["A1"].GetStyle();
Font font;
font.SetBold(true);
font.SetColor(Color::FromArgb(255, 255, 255, 255));
headerStyle.SetFont(font);
headerStyle.SetPattern(FillPattern::Solid);
headerStyle.SetForegroundColor(Color::FromArgb(255, 34, 120, 212));
sheet.GetCells()["A1"].SetStyle(headerStyle);
sheet.GetCells()["B1"].SetStyle(headerStyle);

Step 7: Merge Cells

Use Cells::Merge to combine a range into a single cell:

CellArea titleArea = CellArea::CreateCellArea("A1", "B1");
sheet.GetCells().Merge(0, 0, 1, 2);

Step 8: Save the Workbook

Call workbook.Save() with a filename to write the spreadsheet as an XLSX file, then call Dispose() to release resources:

workbook.Save("sales.xlsx");
workbook.Dispose();

Common Issues and Fixes

Style not applied after save Always call Cell::SetStyle(style) after modifying the style object. Modifying the returned style directly does not apply changes; you must explicitly set it back.

Formula stored as string literal Use SetFormula("=SUM(...)") not PutValue("=SUM(...)"). PutValue treats the argument as a literal string, not a formula.

Merged cell not rendering correctly Ensure the merge parameters (row, column, rowCount, columnCount) correctly span the intended range. Overlapping merges are not supported.

Workbook has no sheets after construction A default sheet is always created by Workbook(). Call GetWorksheets()[0] to access it.

Dispose not called Always call workbook.Dispose() to release resources, especially in long-running applications.

Frequently Asked Questions

Can I add multiple worksheets to one workbook?

Yes. Use Workbook::GetWorksheets() to access the collection and add sheets programmatically. Ensure each sheet has a unique name using Workbook::EnsureUniqueSheetName().

How do I set a number format for a cell?

Use Style::SetNumberFormat(format) before calling Cell::SetStyle(style). Format strings follow standard Excel number format syntax.

Can I apply the same style to multiple cells?

Yes. Obtain the style once, modify it, then call SetStyle(style) on each target cell.

Does the formula get evaluated when saved?

No. Formula evaluation happens in Excel or a compatible reader when the file is opened. The library writes the formula string to the cell.

How many rows and columns can a worksheet hold?

Each worksheet holds data across its full grid of rows and columns, following standard spreadsheet dimensions.

See Also

 English