How to Create and Populate a Spreadsheet Data Model in C++
Aspose.Cells FOSS for C++ enables creating spreadsheet data models from scratch — adding cell values, setting formulas, merging cell ranges, and applying rich styles — all without Microsoft Excel. This guide covers the core Workbook, Worksheet, Cell, and Style classes.
Step-by-Step Guide
Step 1: Set Up the Build
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 "aspose/cells_foss/Workbook.h"
#include "aspose/cells_foss/WorksheetCollection.h"
#include "aspose/cells_foss/Worksheet.h"
#include "aspose/cells_foss/Cell.h"
#include "aspose/cells_foss/Style.h"
#include "aspose/cells_foss/Font.h"
#include "aspose/cells_foss/Color.h"
#include "aspose/cells_foss/CellArea.h"
using namespace Aspose::Cells_FOSS;Step 3: Create a Workbook and Name the Sheet
int main() {
Workbook workbook;
Worksheet& sheet = workbook.GetWorksheets()[0];
sheet.SetName("Sales");
return 0;
}Step 4: Add Cell Values
Use Cell::PutValue to write text, numbers, and booleans:
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
Use Cell::SetFormula for Excel-compatible formulas:
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
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?
The library supports the standard XLSX row and column limits (1,048,576 rows × 16,384 columns per worksheet).