Working with XLSX Packaging
Working with XLSX Packaging
Aspose.Cells FOSS for C++ reads and writes the .xlsx Open XML format through a dedicated serialization layer. The XlsxWorkbookSerializer class orchestrates loading and saving of all workbook parts including worksheets, styles, defined names, document properties, and auto-filters.
Loading and Saving XLSX Files
Use the Workbook class for all load and save operations. Internally, XlsxWorkbookSerializer handles the Open XML packaging:
#include "aspose/cells_foss/Workbook.h"
using namespace Aspose::Cells_FOSS;
int main() {
// Load existing XLSX
Workbook workbook("input.xlsx");
// Modify content...
// Save back to XLSX
workbook.Save("output.xlsx");
return 0;
}Document Properties
XLSX files carry core and extended document properties (title, author, creation date). These are managed by XlsxDocumentProperties, which builds and loads the docProps/core.xml and docProps/app.xml parts:
| Part | Managed by | Contains |
|---|---|---|
docProps/core.xml | XlsxDocumentProperties::BuildCorePropertiesDocument | Title, author, created, modified |
docProps/app.xml | XlsxDocumentProperties::BuildExtendedPropertiesDocument | Application name, version |
Workbook Metadata and Settings
Workbook-level metadata — calculation settings, book views, and workbook protection — is managed by XlsxWorkbookProperties:
BuildWorkbookPropertiesElement— writes workbook calculation and iteration settingsBuildWorkbookProtectionElement— writes workbook-level password protectionBuildBookViewsElement— writes window position and active sheet settings
Styles and Number Formats
The styles part (xl/styles.xml) is managed by XlsxWorkbookStyles. It maps Style objects from the in-memory model to serialized font, fill, border, and number-format records:
BuildStylesheet— serializes all styles to XMLLoadStylesheet— reads styles from an existing XLSX fileStylesEqual— checks whether two style objects would produce identical XML
Auto-Filter Packaging
Auto-filter state per worksheet is serialized by XlsxWorkbookAutoFilter:
BuildAutoFilterElement— writes the<autoFilter>XML elementLoadAutoFilter— reads auto-filter configuration from a worksheet part
Defined Names
Named ranges and formulas are packaged in the workbook part and loaded by XlsxWorkbookDefinedNames:
BuildDefinedNames— writes allDefinedNameentries to the workbook XMLLoadWorkbookDefinedNames— reads defined names during file load
Tips and Best Practices
- Use
Workbook::Save(fileName)as the single exit point — do not invoke serializer classes directly unless extending the library. - The
.xlsxformat is the only fully supported format; saving to other formats is not available in v0.1. - File paths passed to
Workbook(filePath)orSave(filePath)must use the platform path separator and be fully qualified when running in environments where the working directory may vary. - Workbook protection applied via
XlsxWorkbookProperties::BuildWorkbookProtectionElementis independent of worksheet-level protection set throughWorksheet::Protect.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| File fails to load | Invalid or encrypted XLSX | Ensure the file is a plain (non-password-protected) XLSX |
| Styles lost after save | Style not applied before Save | Call Cell::SetStyle(style) before Workbook::Save |
| Defined names missing after round-trip | Scope set to wrong sheet | Use DefinedName::SetFormula with full range address |
API Reference Summary
| Class/Method | Description |
|---|---|
Workbook(filePath) | Load an XLSX file |
Workbook::Save(fileName) | Save to XLSX |
XlsxWorkbookSerializer::Save | Internal: serializes model to Open XML |
XlsxWorkbookSerializer::Load | Internal: deserializes Open XML to model |
XlsxDocumentProperties | Core and extended document property packaging |
XlsxWorkbookProperties | Workbook metadata and settings serialization |
XlsxWorkbookStyles | Style sheet packaging |
XlsxWorkbookAutoFilter | Auto-filter XML packaging |
XlsxWorkbookDefinedNames | Defined name XML packaging |