Quick Start

Overview

This walkthrough creates a workbook, writes data and a formula, styles the header row, saves the file, then reloads it with repair options enabled.


Step 1: Create a Workbook and Write Data

Create a new Workbook, get its first worksheet from the Worksheets collection, and write string and numeric values into cells by key (e.g. 'A1'). Setting a cell’s Formula property, as shown for B4, stores a formula string that Excel evaluates when the file is opened.

<?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)';

Step 2: Style the Header Row

Read the existing style object for a header cell with GetStyle(), set the font to bold with a white color, apply a solid fill pattern, and set the foreground (fill) color, then reapply the style to both header cells with SetStyle().

$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);

Step 3: Save the Workbook

Write the workbook to disk by calling Save() with a target file path; the output format is inferred from the file extension.

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

Step 4: Reload with Repair Options

Reopen a workbook with a LoadOptions object configured to tolerate and repair a damaged package or XML content (TryRepairPackage, TryRepairXml) with StrictMode disabled. Check LoadDiagnostics->HasDataLossRisk before continuing to edit and resave the file.

<?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('products.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('products.xlsx');

Next Steps

  • Developer Guide: Core cell operations, packaging, navigation, and validation
  • Features: Deep-dive into every feature area

See Also