How to Work with Packaging in PHP

How to Work with Packaging in PHP

An .xlsx file is an OOXML ZIP package. Workbook::Save() and the Workbook loading constructor handle this package structure automatically — most applications never need the classes covered here directly.

Step-by-Step Guide

Step 1: Install the Package

git clone https://github.com/aspose-cells-foss/Aspose.Cells-FOSS-for-PHP.git
cd Aspose.Cells-FOSS-for-PHP
composer install

Step 2: Import Required Classes

use Aspose\Cells_FOSS\Workbook;

Step 3: Let Workbook Handle Packaging for You

For everyday use, work through Workbook — it manages the underlying PackageModel for you:

$workbook = new Workbook();
$sheet = $workbook->Worksheets[0];
$sheet->Cells['A1']->PutValue('Packaged automatically');
$workbook->Save('report.xlsx');

Step 4: Recognize the Lower-Level Classes

If a load fails with a package-structure error, the library’s internal layer exposes PackageModel (the raw package: Parts, Relationships, UnsupportedParts), PackageLoadContext (ties a loaded workbook to its source package), and the IPackageReader/IPackageWriter interfaces the library implements internally. PackagingConventions supplies well-known OPC part names, for example:

use Aspose\Cells_FOSS\Packaging\PackagingConventions;

echo PackagingConventions::WorkbookPart();

Common Issues and Fixes

A load throws a package-structure exception. Instead of inspecting the package model directly, load with LoadOptions->TryRepairPackage set to true — this handles most malformed or incomplete OPC packages.

Frequently Asked Questions

Do I need to use PackageModel or PackageLoadContext directly?

No, not for typical application code. They are the implementation layer Workbook uses internally; reach for them only if you are diagnosing a package-structure error that LoadOptions repair cannot resolve.

See Also