Cookbook

Download the generated XLSX

The saveXlsx phpxlsx method generates XLSX files in the file system, but does not download them on the spot, for example, when using a browser.

To generate and download the output files you can either choose the methods available in the library or work with some custom PHP code.

The saveXlsxAndDownload method

All phpxlsx licenses include the saveXlsxAndDownload method to create and download the XLSX. A simple example of use of this method would be as follows:

The download of the files happens thanks to a set of headers for XLSX files:

Alternative download code

The headers of the previous method are the standard ones for XLSX files, and so, are compatible with the majority of servers. However, in case of not functioning correctly, it is possible to work with any other method and headers to download the files. In this case, the first thing to do is generating the XLSX with the saveXlsx method, and then typing a custom code for the download.

An alternative method for the download would be the PHP ob_ functions, e.g.:

Delete files after the download

The saveXlsxAndDownload method creates and downloads an XLSX, but the default options don't delete it. If you want to do so after the download, you must set the second parameter as true.

E.g., to generate and download an XLSX, and delete it afterwards, write this PHP code:

Streams of files (Premium licenses only)

The package of the Premium licenses integrates the option of XLSX generation as a stream, that is, without saving the XLSX in the system files, and thus obtaining it directly in the browser or getting the output in the CLI mode.

You can set up dinamically this stream way in a script, or force it for all files in the configuration file config/phpxlsxconfig.ini.

The following example sets up the stream mode of phpxlsx:

The CLI mode with streams allows to redirect the output, e.g., to a file: