Create Excel Files from Scratch using PHP

Create Excel files in PHP

Spreadsheets have become an essential part of keeping, organizing, and analyzing the data. Since automated solutions are more in business these days, the trend of creating and manipulating Excel documents (XLS/XLSX) has emerged and growing at a huge pace. In accordance with the above-mentioned scenario, this article covers how to create Excel XLSX or XLS files in PHP based web applications.

PHP API to Create Excel Files

In order to create and manipulate Excel files in PHP based web applications, we will use Aspose.Cells for PHP via Java. It is a powerful API that provides a wide range of features for Excel automation. You can download the API package from here.

Usage

The following are the prerequisites that you need to fulfill in order to use Aspose.Cells for PHP via Java.

Once you have completed the prerequisites, follow the below steps to execute the example.php file for testing.

  1. Place Java.inc file in the root folder of the API’s package that you have downloaded.
  2. Run JavaBridge.jar using the below commands in the command prompt:
    • > cd aspose.cells
    • > %JAVA_HOME%\bin\java -Djava.ext.dirs=lib -jar JavaBridge.jar SERVLET_LOCAL:8080
  3. Run example.php from the API’s root folder using the below command:
    • > php example.php

Create Excel XLS or XLSX Files using PHP

The following are the steps to create an Excel XLSX/XLS file using PHP.

The following code sample shows how to create an Excel XLSX file using PHP.

Write Data to Existing Excel File using PHP

In the previous section, we created an Excel file from scratch. Now, let’s edit an existing Excel file and insert data into it. The following are the steps to write data to an XLSX file using Aspose.Cells for PHP via Java.

The following code sample shows how to modify an Excel file and write data into it using PHP.

Create Charts in an Excel File using PHP

The following are the steps to create charts in an Excel file using PHP.

The following code sample shows how to create charts in Excel files in PHP.

Create a Pivot Table in an Excel File using PHP

Pivot tables in Excel worksheets are used for adding filters to the data, computing totals, summarizing data, etc. Pivot tables can be created using the range of the cells in the worksheet. The following are the steps to create a pivot table in an Excel worksheet using PHP.

The following code sample shows how to create a pivot table in Excel using PHP.

Get a Free API License

You can use the API for free without evaluation limitations using a temporary license.

Conclusion

In this article, you have learned how to create Excel files from scratch using PHP. Furthermore, you have seen how to write data to an existing Excel file and generate charts or tables. You can explore more about the PHP Excel API using the documentation. In case you would have any questions, feel free to let us know via our forum.

See Also