Create Excel files in PHP

Excel spreadsheets are a core tool for businesses and developers who need to manage, analyze, and report data. Automating Excel file creation and manipulation directly from PHP code is a frequent requirement for modern web applications. In this post we’ll demonstrate how to create, edit, and enhance Excel files in PHP using Aspose.Cells for PHP, a comprehensive library that delivers full Excel automation without requiring Microsoft Office.

PHP Excel Library

Aspose.Cells for PHP is a powerful, fully‑featured PHP library that lets developers generate, modify, and convert Excel workbooks programmatically. It supports creating worksheets, applying cell styles, inserting charts, building pivot tables, and performing advanced data analysis—all from within PHP code. Using Aspose.Cells for PHP you can automate any Excel‑related task without installing Microsoft Office on the server.

Download the latest library package from here.

Usage

Before you start, ensure the following prerequisites are satisfied:

After installing the prerequisites, follow these steps to run the provided example.php script:

  1. Place the Java.inc file in the root directory of the downloaded Aspose.Cells package.
  2. Start the Java bridge with the commands below:
    • cd aspose.cells
    • %JAVA_HOME%\bin\java -Djava.ext.dirs=lib -jar JavaBridge.jar SERVLET_LOCAL:8080
  3. Execute the sample script from the library root:
    • php example.php

Create an Excel File in PHP

The process of generating a new Excel workbook in PHP is straightforward with Aspose.Cells. Follow these steps:

  • Instantiate a Workbook object.
  • Retrieve the WorksheetCollection via $workbook->getWorksheets().
  • Select the target worksheet and obtain its Cells collection with $worksheets->get(index)->getCells().
  • Write data to a cell using $cells->get("A1")->putValue("Hello world!").
  • Save the workbook in the desired format, for example $workbook->save("output.xlsx", cells\SaveFormat::XLSX).

The code snippet below creates a simple XLSX file and writes “Hello world!” to cell A1.

Edit an Excel File in PHP

After creating a workbook, you often need to update an existing file with new data. The steps below show how to open an existing XLSX file and modify its contents:

  • Load the file by creating a Workbook instance with the file path.
  • Access the file’s WorksheetCollection using $workbook->getWorksheets().
  • Get the target worksheet’s Cells collection via $worksheets->get(index)->getCells().
  • Insert or update a cell value with $cells->get("A1")->putValue("Hello world!").
  • Save the changes using $workbook->save("output.xlsx", cells\SaveFormat::XLSX).

The following example demonstrates how to edit an existing Excel file in PHP.

Add a Chart in an Excel File

Charts are essential for visualizing data trends. Use Aspose.Cells for PHP to add a chart to any worksheet:

  • Create a new workbook or load an existing one with the Workbook class.
  • Populate the worksheet with the data series you want to chart.
  • Retrieve the chart collection via $worksheet->getCharts().
  • Add a new chart using $worksheet->getCharts()->add(type, row, column, height, width).
  • Configure the chart’s NSeries by specifying the cell range that contains the data.
  • Save the workbook as an XLSX file with $workbook->save("output.xlsx", cells\SaveFormat::XLSX).

The code sample below illustrates how to generate a column chart in an Excel file using PHP.

Add a Pivot Table in an Excel File in PHP

Pivot tables provide powerful data summarization and filtering capabilities. To create a pivot table with PHP:

  • Open or create a workbook using the Workbook class.
  • Insert the source data into the worksheet.
  • Access the pivot table collection through $worksheet->getPivotTables().
  • Add a new pivot table with $worksheet->getPivotTables()->add(pivotTable, "A1", "B10").
  • Define the data fields, row fields, and column fields for the pivot table.
  • Save the workbook using $workbook->save("output.xlsx", cells\SaveFormat::XLSX).

The following snippet shows how to build a pivot table in an Excel worksheet via PHP.

Online Excel Editor

Aspose also offers a free online Excel editor that lets you create and edit Excel files directly in the browser—no registration required.

Image

Get Free PHP Excel Library

You can obtain a free temporary license to use Aspose.Cells for PHP without evaluation restrictions. Grab it from the free temporary license page.

Conclusion

Aspose.Cells for PHP delivers a complete solution for generating, editing, and enhancing Excel workbooks in PHP applications. This article covered the fundamentals of creating Excel files, inserting data, adding charts, and building pivot tables. The library also supports advanced operations such as numerical calculations, data analysis, and format conversion, making it an ideal choice for any PHP project that requires robust Excel automation. For deeper guidance, consult the official Aspose documentation or ask questions in our forum.

See Also