How to load and operate with XLSX files in PHP

Working with Excel files in PHP has become a common necessity for many web developers. Whether it's importing data from spreadsheets, generating reports, or manipulating existing files, having the right tools can significantly ease the process. In this guide, we'll explore how to load and operate with XLSX files using the XlsxObject class from the TurboDepot library in PHP.

Introduction to XlsxObject Class

The XlsxObject class is part of the TurboDepot library, designed to provide a convenient way to work with Excel XLSX files in PHP. This class allows you to read and manipulate data from XLSX spreadsheets with ease. It utilizes the SimpleXMLElement class for XML parsing and provides methods to interact with individual sheets within an XLSX file.

Prerequisites

Before proceeding, ensure that the following requirements are met:

Installing the TurboDepot library

Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies. To begin working with an XLSX file, instantiate the XlsxObject class and provide the binary data of the XLSX file as input. Here's how you can load an XLSX file:

<?php

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\model\XlsxObject;

// Load XLSX file
$binaryData = file_get_contents('path/to/your/file.xlsx');
$xlsx = new XlsxObject($binaryData);

Replace 'path/to/your/file.xlsx' with the actual path to your XLSX file.

Operating with XLSX Data

Once the XLSX file is loaded into the XlsxObject instance, you can perform various operations such as accessing sheets, retrieving cell values, and manipulating data. Here are some common operations you can perform:

Set Active Sheet

To set the active sheet within the XLSX file, use the setActiveSheet method. Specify the index of the sheet you want to activate. At startup, the default sheet is always the first one.

// Set active sheet
$xlsx->setActiveSheet(0); // Activate the first sheet (index 0)

Count Sheets

To determine the number of sheets present in the XLSX file, use the countSheets method.

// Count sheets
$numSheets = $xlsx->countSheets(); // Get the total number of sheets

Retrieve Cell Values

You can retrieve the value of a specific cell within the active sheet using the getCell method.

// Retrieve cell value
$cellValue = $xlsx->getCell($rowIndex, $colIndex); // Get value at specified row and column index

Count number of cells, rows and columns

You can easily retrieve the total number of different elements on the xlsx file:

$totalCells = $xlsx->countCells();
$totalRows = $xlsx->countRows();
$totalColumns = $xlsx->countColumns();

Replace $rowIndex and $colIndex with the row and column indices, respectively.

Additional Operations

The XlsxObject class provides additional methods for working with XLSX data, such as checking if a value contains valid XLSX data (isXlsx method), and more.

Conclusion

In this guide, we've learned how to load and operate with XLSX files in PHP using the XlsxObject class from the TurboDepot library. By following these steps, you can seamlessly integrate Excel file handling into your PHP applications, allowing for efficient data processing and manipulation.