Get field data when using Spout to read an Excel file
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Data Extraction: Getting Field Data from Excel using Spout
Reading structured data from external files like Excel spreadsheets is a common requirement in application development. When using tools like Spout to parse these files, the primary challenge often lies in efficiently mapping the raw cell data back into meaningful, accessible fields within your programming logic. Many developers, when first starting with file parsers, look for an intuitive property access, such as $row->email, but this doesn't always work out of the box.
This post will walk you through the correct method for accessing specific fields from Excel data read via Spout, providing practical solutions and best practices.
The Challenge: Understanding How Spout Reads Data
You are using code similar to this to read your Excel file:
$filePath = '/Users/nhathao/Desktop/employee-test.xlsx';
$reader = ReaderEntityFactory::createXLSXReader();
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
echo "<pre>";
print_r($row->getCells()); // This outputs an array of cell objects/data
echo "</pre>";
}
}
As you observed, the output from methods like $row->getCells() provides a comprehensive view of all cells in that row. However, it doesn't automatically provide named fields like email or name. This is because the Excel file itself is purely positional—it stores data based on row and column indices (A1 notation), not semantic field names.
To access specific fields, we need to use the column index provided by Spout and manually map it to the desired data point.
The Solution: Mapping Indices to Fields
The key to solving this is understanding that when you iterate through the cells of a row, the data resides at a specific numerical position within the collection returned by getCells(). If your Excel file has columns A (index 0), B (index 1), C (index 2), and so on, you can use these indices to retrieve the correct value.
For instance, if 'Email' is in column B (index 1) and 'Name' is in column A (index 0), you must access the data using those numerical positions.
Here is how you can refactor your loop to extract specific fields:
$filePath = '/Users/nhathao/Desktop/employee-test.xlsx';
$reader = ReaderEntityFactory::createXLSXReader();
$reader->open($filePath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
// Access the cells array for the current row
$cells = $row->getCells();
// Assuming Column A is index 0 (Name) and Column B is index 1 (Email)
if (count($cells) > 1) {
$name = $cells[0]; // Data from Column A
$email = $cells[1]; // Data from Column B
// Now you have the specific data you need!
echo "Name: " . $name . "\n";
echo "Email: " . $email . "\n";
// Store the email for database comparison
$employeeEmail = $email;
}
}
}
Best Practice: Creating a Data Structure
Instead of just echoing the data, the best practice is to collect this extracted information into an array or an object. This mirrors how data is structured in modern applications. If you were building a data pipeline similar to what powers robust systems like those discussed on Laravel Company, structuring your results upfront makes subsequent processing much cleaner and more maintainable.
You could store the results in a master array:
$employeeData = [];
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
if (count($row->getCells()) > 1) {
// Assuming Email is the second column (index 1)
$email = $row->getCells()[1];
$employeeData[] = [
'name' => $row->getCells()[0],
'email' => $email,
// Add other fields here...
];
}
}
}
// Now $employeeData holds all your structured records.
print_r($employeeData);
Conclusion
The confusion around accessing specific data points in spreadsheet parsers stems from the difference between positional data (what the file stores) and semantic data (what you want to call it). By explicitly using the column indices provided by the reader and structuring your extracted results into associative arrays, you transform raw cell data into meaningful variables. This approach ensures that your application logic remains decoupled from the specific layout of the Excel sheet, making your code robust, readable, and ready for integration with databases or other services.