Generate Documents in PHP With an Excel Template

0
41
laravel-excel.png


The AnourValar/office package is a PHP +8.1 library to generate documents from XLSX templates. Given a template like the following example template (from the readme):

Excel template example

Using the following code you can generate a document using the template values:

1$data = [

2 // scalar

3 'vat' => 'No',

4 'total' => [

5 'price' => 2004.14,

6 'qty' => 3,

7 ],

8 

9 // one-dimensional table

10 'products' => [

11 [

12 'name' => 'Product #1',

13 'price' => 989,

14 'qty' => 1,

15 'date' => new \DateTime('2022-03-30'),

16 ],

17 [

18 'name' => 'Product #2',

19 'price' => 1015.14,

20 'qty' => 2,

21 'date' => new \DateTime('2022-03-31'),

22 ],

23 ],

24];

25 

26// Save as XLSX (Excel)

27(new \AnourValar\Office\TemplateService())

28 ->generate(

29 'template1.xlsx', // path to template

30 $data // input data

31 )

32 ->saveAs(

33 'generated_document.xlsx', // path to save

34 \AnourValar\Office\Format::Xlsx // save format

35 );

Which produces the following based on the template1.xlsx file:

Generated Excel document from a template

When saving the document, this package supports multiple save formats, including:

Apart from generating files from XLSX templates, this package also supports:

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

See the README for further details on how to use this package. You can learn more about this package, get full installation instructions, and view the source code on GitHub.



Source link

Leave a reply

Please enter your comment!
Please enter your name here