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 and modify Excel files in PHP.

PHP Library to Create Excel Files - Free Download

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 and high-speed library that provides a wide range of features for Excel automation. You can download the library 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 library’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 library’s root folder using the below command:
    • php example.php

Easy Steps to Create Excel File in PHP

The following are some easy steps to create an Excel file in PHP.

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

Modify an Excel File in 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 in PHP.

PHP: Create Excel File with Charts

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.

Add a Pivot Table in an Excel File in 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.

Aspose’ PHP Library to Create Excel Files - Get a Free License

You can create Excel files in PHP for free without evaluation limitations using a free temporary license.

Conclusion

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

See Also