Aspose.Cells FOSS for PHP Use Cases

Aspose.Cells FOSS for PHP Use Cases

Two common scenarios this library supports today, grounded in its verified public API.

Generating a Styled Report

Create a workbook, write a small dataset and a sum formula, apply header styling, then save it as .xlsx:

<?php
require __DIR__ . '/vendor/autoload.php';

use Aspose\Cells_FOSS\Color;
use Aspose\Cells_FOSS\FillPattern;
use Aspose\Cells_FOSS\Workbook;

$workbook = new Workbook();
$sheet = $workbook->Worksheets[0];

$sheet->Name = 'Products';
$sheet->Cells['A1']->PutValue('Product');
$sheet->Cells['B1']->PutValue('Price');
$sheet->Cells['A2']->PutValue('Apple');
$sheet->Cells['B2']->PutValue(2.99);
$sheet->Cells['A3']->PutValue('Orange');
$sheet->Cells['B3']->PutValue(1.99);
$sheet->Cells['B4']->Formula = '=SUM(B2:B3)';

$headerStyle = $sheet->Cells['A1']->GetStyle();
$headerStyle->Font->IsBold = true;
$headerStyle->Font->Color = Color::FromArgb(255, 255, 255, 255);
$headerStyle->Pattern = FillPattern::Solid;
$headerStyle->ForegroundColor = Color::FromArgb(255, 34, 120, 212);

$sheet->Cells['A1']->SetStyle($headerStyle);
$sheet->Cells['B1']->SetStyle($headerStyle);

$workbook->Save('products.xlsx');

Safely Reloading a Workbook of Unknown Quality

When a workbook comes from an untrusted source (a user upload, for example), load it with repair options enabled and check the diagnostics before trusting its contents:

<?php
require __DIR__ . '/vendor/autoload.php';

use Aspose\Cells_FOSS\LoadOptions;
use Aspose\Cells_FOSS\Workbook;

$loadOptions = new LoadOptions();
$loadOptions->TryRepairPackage = true;
$loadOptions->TryRepairXml = true;
$loadOptions->StrictMode = false;

$workbook = new Workbook('input.xlsx', $loadOptions);

if ($workbook->LoadDiagnostics->HasDataLossRisk) {
    echo 'Potential data loss risk detected during load.' . PHP_EOL;
}

$workbook->Worksheets[0]->Cells['A1']->PutValue('Updated');
$workbook->Save('updated.xlsx');

See Also