33.2 C
Pakistan
Saturday, July 27, 2024

Create PHP Documents Using an Excel Template

A PHP +8.1 library called AnourValar/office is used to create documents using XLSX templates. Using a template similar to the one below as an example (found in the readme):

You can create a document with the template values by using the code below:

$data = [
    // scalar
    'vat' => 'No',
    'total' => [
        'price' => 2004.14,
        'qty' => 3,
    ],
 
    // one-dimensional table
    'products' => [
        [
            'name' => 'Product #1',
            'price' => 989,
            'qty' => 1,
            'date' => new \DateTime('2022-03-30'),
        ],
        [
            'name' => 'Product #2',
            'price' => 1015.14,
            'qty' => 2,
            'date' => new \DateTime('2022-03-31'),
        ],
    ],
];
 
// Save as XLSX (Excel)
(new \AnourValar\Office\TemplateService())
    ->generate(
        'template1.xlsx', // path to template
        $data // input data
    )
    ->saveAs(
        'generated_document.xlsx', // path to save
        \AnourValar\Office\Format::Xlsx // save format
    );

Which, using the template1.xlsx file as a basis, generates the following:

This package supports a variety of save formats for the document, such as:

  • XLSX
  • PDF
  • HTML
  • ODS

This package does not only generate files from XLSX templates; it also supports:

  • Two-dimensional tables
  • Image insertion
  • Dynamic templates
  • Merge multiple documents into a single file
  • Export Table (grid service)

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles