Practical phpxlsx

Creating a new spreadsheet

Introduction

Once the phpxlsx installation is completed, you can work with the library. To begin with, we are going to generate a spreadsheet from scratch, that is, without an external template.

To do this, you'll create an example as simple as possible in which you'll add a cell content and then, save the spreadsheet with a name of your choice.

The version that supports the namespaces package is available for the Advanced and Premium licenses.

Due to phpxlsx internal performance, it is required to instance a new object for every file you generate. Otherwise, there may be unexpected results in the file content.

No-namespaces version

The full code for creating a XLSX is the following:

First, include the main class of phpxlsx CreateXlsx:

This class loads automatically the rest of the available classes through an autoloader.

Then, create a new spreadsheet:

You are going to add a content in a cell, so use an array:

... and then add it to the spreadsheet with the method addCell:

To, finally generate the XLSX:

With the file name output.xlsx

Namespaces version, PHP 5.3 or higher

The full code for creating a spreadsheet is as follows:

The differences between this and the no-namespaces version are limited to how you include the library and classes.

Include the main class, as it manages the loading of the rest of the classes:

Now you can create the new spreadsheet:

If you're using a framework based on Composer such as Symfony, Laravel..., you need the namespaces package (available in Advanced and Premium licenses). You can also learn more about using phpxlsx with frameworks and CMS in the Cookbook.

Destination path/route

When generating the XLSX with the saveXlsx method, you can specify a relative or absolute path, to indicate the name and path where the file is going to be saved.

For example:

saves the XLSX in the script execution directory.

generates the XLSX in the sub-directory files, located one level above the current directory; or

saves it in the path /var/phpxlsx/files.

Before setting a path for saving your files, it is necessary to check that the execution user has the required read and write access rights.

Downloading the XLSX

phpxlsx includes the saveXlsxAndDownload method to generate and download the XLSX in just one step:

For example:

This download uses the following headers:

These headers work in most configurations, but in some cases might be insufficient. When this happens, it is necessary to create the XLSX with the saveXlsx method and use your own download headers.

Documentation

With each package of phpxlsx, full documentation comes in phpxlsx file format, which is the main reference to know in depth the software library. All public methods go along with a description of each of the available parameters.

In addition, it is advisable to visit:

Other useful resources

For further information and to solve doubts about the use of the library, there are several resources, both internal and external:

In addition, it is advisable to visit:

  • phpxlsx forum

    Forum of the great community of phpxlsx users, to solve doubts and queries

  • Contact

    Contact form, for direct communication with the phpxlsx team

  • Stack Overflow

    Reference forum for developers

Next - Adding contents