How to Load a Spreadsheet in C++

How to Load a Spreadsheet in C++

Aspose.Cells FOSS for C++ allows you to load and read existing XLSX spreadsheets without requiring Microsoft Excel. This guide walks through loading a workbook, accessing worksheets, and reading cell values using the Workbook, Worksheet, and Cell classes.

Step-by-Step Guide

Step 1: Set Up the Build

Add Aspose.Cells FOSS for C++ to your CMake project:

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

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

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

Step 2: Include Required Headers

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

using namespace Aspose::Cells_FOSS;

Step 3: Load the Workbook

Pass the file path to the Workbook constructor to load an existing XLSX file:

int main() {
    Workbook workbook("input.xlsx");
    // workbook is now loaded and ready to use
    return 0;
}

Step 4: Access a Worksheet

Use Workbook::GetWorksheets() to retrieve the worksheet collection, then access by index:

Worksheet& sheet = workbook.GetWorksheets()[0];
std::string name = sheet.GetName();

Step 5: Read Cell Values

Access cells by address string and read their values:

std::string text = sheet.GetCells()["A1"].GetStringValue();
double number = sheet.GetCells()["B2"].GetValue().AsDouble();
bool flag = sheet.GetCells()["C3"].GetValue().AsBool();

Step 6: Save Any Changes

After making modifications, save back to a file:

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

Common Issues and Fixes

File not found error on load Verify the file path is correct and the file exists. Use an absolute path if the working directory is uncertain.

Empty string returned for a numeric cell Use GetValue().AsDouble() instead of GetStringValue()GetStringValue() returns an empty string for cells that contain numbers rather than text.

Workbook loads but cells appear empty Ensure the XLSX file is not password-protected. Aspose.Cells FOSS for C++ does not support encrypted files.

Formula cell returns unexpected value Formula cells store the formula string, not the computed value. The formula is evaluated by Excel when the file is opened. Use GetFormula() to retrieve the formula text.

Memory not released after processing Call workbook.Dispose() after all operations are complete to release unmanaged resources.

Frequently Asked Questions

Can I load multiple worksheets from a single file?

Yes. Use Workbook::GetWorksheets() to access all sheets and iterate by index. Call sheet.GetName() to identify each sheet by name.

Can I load XLSX files with multiple sheets and read from a specific one?

Yes. Access sheets by zero-based index: workbook.GetWorksheets()[1] returns the second sheet.

Does loading a file trigger formula recalculation?

No. The library loads cell values and formula strings as stored in the XLSX file. Formula recalculation happens in Excel or a compatible reader when the file is opened.

What happens if the file path contains Unicode characters?

Pass the path as a std::string with the correct encoding for your platform. UTF-8 paths are supported on Linux and macOS. On Windows, ensure the path encoding matches the system locale.

Is it safe to load the same file multiple times concurrently?

The library is not thread-safe for concurrent access to the same Workbook instance. Load separate Workbook instances per thread.

See Also