Working with Spreadsheet Management

Working with Spreadsheet Management

Working with Spreadsheet Management

The Workbook class is the entry point for all spreadsheet operations in Aspose.Cells FOSS for C++. It provides methods to create new workbooks, load existing XLSX files, manage worksheets, and save the result.


Creating a New Workbook

Instantiate Workbook to create an empty workbook. A default worksheet is created automatically.

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

using namespace Aspose::Cells_FOSS;

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

Loading an Existing Workbook

Pass a file path to the Workbook constructor to load an existing XLSX file. After loading, worksheets and cell data are accessible immediately.

Workbook workbook("existing.xlsx");
Worksheet& sheet = workbook.GetWorksheets()[0];
// Access or modify cells here
workbook.Save("modified.xlsx");

Configuring Worksheet Properties

The Worksheet class exposes display and protection properties. Use these to configure sheet visibility, grid display, zoom, and tab color.

Worksheet& sheet = workbook.GetWorksheets()[0];
sheet.SetName("Data");
sheet.SetShowGridlines(false);
sheet.SetShowRowColumnHeaders(true);
sheet.SetZoom(80);

Protecting and Unprotecting a Worksheet

Call Worksheet::Protect to lock a sheet against edits, and Worksheet::Unprotect to remove protection:

sheet.Protect("password");
// ... distribute the workbook ...
sheet.Unprotect("password");

Ensuring Unique Sheet and Defined-Name Scope

When adding multiple worksheets programmatically, use Workbook::EnsureUniqueSheetName to avoid name collisions. Use Workbook::EnsureValidDefinedNameScope to validate that defined names are scoped correctly.

workbook.EnsureUniqueSheetName();
workbook.EnsureUniqueDefinedName();

Saving a Workbook

Call Workbook::Save(fileName) to write the workbook to disk. Multiple overloads accept a file name, a stream, or an explicit SaveFormat.

workbook.Save("products.xlsx");

Tips and Best Practices

  • Always call Workbook::Dispose() when done with a workbook to release unmanaged resources.
  • Worksheet names must be unique within a workbook — use EnsureUniqueSheetName after batch additions.
  • Set Worksheet::SetShowGridlines(false) for presentation-style sheets to improve readability.
  • Use Worksheet::SetZoom(value) to set a default zoom level (percentage) for worksheet views.
  • Tab colors assigned via Worksheet::SetTabColor help users distinguish worksheets visually.

Common Issues

IssueCauseFix
File not found on loadIncorrect pathUse absolute path or verify working directory
Sheet name collisionDuplicate names when adding sheetsCall EnsureUniqueSheetName() after each addition
Workbook changes not savedSave not calledAlways call workbook.Save(path) before exit
Memory leak warningsDispose not calledCall workbook.Dispose() at end of scope

API Reference Summary

Class/MethodDescription
Workbook()Creates a new empty workbook
Workbook(filePath)Loads an existing XLSX file
Workbook::Save(fileName)Saves the workbook to disk
Workbook::EnsureUniqueSheetName()Resolves duplicate sheet names
Workbook::Dispose()Releases resources
Worksheet::SetName(name)Renames the worksheet
Worksheet::SetShowGridlines(bool)Toggles grid line visibility
Worksheet::SetZoom(percent)Sets default zoom percentage
Worksheet::Protect(password)Locks the worksheet
Worksheet::Unprotect(password)Unlocks the worksheet

See Also