Working with Excel files is a common requirement in many web development projects. PHP 8 provides several techniques and libraries that allow you to convert Excel files into table columns for further processing. In this article, we will explore some examples for converting an Excel file into table columns using PHP 8.

 php excel conversion

Using the PhpSpreadsheet Library:

PhpSpreadsheet is a powerful PHP library for reading and writing spreadsheet files, including Excel files. To convert an Excel file into table columns, you can utilize the PhpSpreadsheet library. Here's an example:

//...

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

// Load the Excel file
$spreadsheet = IOFactory::load('example.xlsx');

// Get the active sheet
$sheet = $spreadsheet->getActiveSheet();

// Get the highest column index
$highestColumnIndex = $sheet->getHighestColumn();

// Convert Excel columns to an array
$columns = range('A', $highestColumnIndex);

// Display the columns
foreach ($columns as $column) {
    $columnValues = $sheet->getColumnValues($column);
    echo "Column $column: " . implode(", ", $columnValues) . "<br>";
}

//...

In this example, we first require the PhpSpreadsheet library and load the Excel file using `IOFactory::load()`. We then get the active sheet using `$spreadsheet->getActiveSheet()`. Next, we determine the highest column index using `$sheet->getHighestColumn()`. We convert the Excel columns to an array using `range()`, and for each column, we retrieve the values using `$sheet->getColumnValues()` and display them.

 

Using the Spout Library:

Spout is a lightweight PHP library for reading and writing spreadsheet files, including Excel files. To convert an Excel file into table columns using Spout, you can utilize the Spout library. Here's an example:

//...

require 'vendor/autoload.php';

use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;

// Create a reader instance
$reader = ReaderEntityFactory::createXLSXReader();

// Open the Excel file
$reader->open('example.xlsx');

// Get the first sheet
$reader->open('example.xlsx');
$sheet = $reader->getSheetIterator()->current();

// Iterate through each row
foreach ($sheet->getRowIterator() as $row) {
    $rowValues = $row->toArray();
    foreach ($rowValues as $column => $value) {
        echo "Column $column: $value<br>";
    }
}

//...

In this example, we first require the Spout library. We create a reader instance using `ReaderEntityFactory::createXLSXReader()`. We then open the Excel file using `$reader->open()`. We retrieve the first sheet using `$reader->getSheetIterator()->current()`. We iterate through each row and retrieve the values using `$row->toArray()`. Finally, we display the column values.

These examples demonstrate techniques for converting Excel files into table columns using PHP 8. The PhpSpreadsheet and Spout libraries provide powerful features for working with Excel files, allowing you to extract data and transform it into table columns for further processing. Experiment with these examples and integrate them into your web development projects to efficiently handle Excel file conversions.