Call to undefined method PhpOffice\PhpSpreadsheet\Spreadsheet::listWorksheetNames

Call to undefined method PhpOffice\PhpSpreadsheet\Spreadsheet::listWorksheetNames

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The listWorksheetNames() Method Doesn't Exist in PhpSpreadsheet

The error message Call to undefined method PhpOffice\PhpSpreadsheet\Spreadsheet::listWorksheetNames() indicates a fundamental mismatch between your code and the actual API of the PhpSpreadsheet library. This is not a configuration issue, a path problem with your Excel file, or a version-specific quirk that requires complex workarounds; it is a simple case of using a method name that was never implemented in the PhpOffice\PhpSpreadsheet\Spreadsheet class.

In the PhpSpreadsheet ecosystem, the public API follows specific naming conventions, and listWorksheetNames() is not one of them. While the intent behind your code is clear—you want to retrieve an array of sheet names from a loaded workbook—the library provides a different approach that you must adopt to resolve this runtime exception.

The Correct Approach: Using getSheetNames()

The method designed specifically for this purpose is getSheetNames(). This function returns an indexed array containing the names of all worksheets currently loaded in the spreadsheet object. Replacing the non-existent listWorksheetNames() call with getSheetNames() will immediately resolve the "undefined method" error.

Here is how your corrected code should look:

<?php
require_once('vendor/autoload.php');

use PhpOffice\PhpSpreadsheet\IOFactory;

$inputFileName = 'uploads/1.xlsx';
$spreadsheet = IOFactory::load($inputFileName);

// Use getSheetNames() instead of listWorksheetNames()
$worksheetNames = $spreadsheet->getSheetNames();

echo '<h3>Worksheet Names</h3>';
echo '<ol>';
foreach ($worksheetNames as $worksheetName) {
    echo '<li>', htmlspecialchars($worksheetName), '</li>';
}
echo '</ol>';
?>

Why the API is Structured This Way

Understanding why PhpSpreadsheet uses getSheetNames() rather than a list* variant helps when navigating its documentation. The library prioritizes brevity and clarity in method names where possible. For instance, to access a specific sheet by index, you use $spreadsheet->getActiveSheet(), and to get a sheet by name, you use getSheetByIndex($name) or similar variations depending on the context. The get prefix is the standard convention for retrieving data from the object state.

It is also worth noting that listWorksheetNames() might exist in your mind because of other libraries or older documentation styles where list* methods were common (e.g., listFiles, listDirectories). However, in modern PHP versions and specifically within PhpSpreadsheet, this naming convention does not apply to spreadsheet objects.

Handling Potential Pitfalls

When iterating over sheet names, always remember that the order of sheets returned by getSheetNames() corresponds to their visual order in the Excel file (Tab 1, Tab 2, etc.). However, a common pitfall involves how you handle the output. In your original snippet, you echoed the $worksheetName directly inside an HTML <li> tag. If any sheet names contain special characters or are intended for display on a webpage, this can lead to broken HTML entities or XSS vulnerabilities.

Always sanitize and escape output when rendering user-facing content. The htmlspecialchars() function is sufficient for most cases:

echo '<li>', htmlspecialchars($worksheetName), '</li>';

Working with Named Worksheets Only

If your goal is slightly different—for example, if you only want to list sheets that have been explicitly "named" and not just the standard tabs—PhpSpreadsheet offers a different workflow. You can iterate through all sheets using getSheetByName() or checking against the total sheet count, but retrieving a specific list of named ranges requires accessing the DefinedNames collection within each sheet.

For strictly reading file metadata without loading the full workbook into memory, you might consider using the Xlsx reader directly with specific options, though this is rarely necessary for simply listing tab names. The standard approach of loading the file and calling getSheetNames() remains the most robust method for general use cases.

Final Implementation Note

If you are building a larger application, such as a Laravel project, ensure that your service classes or controllers adhere to these API standards. Hardcoding non-existent methods like listWorksheetNames() will cause fatal errors at runtime whenever the file is processed. Always verify the method signature against the official documentation before assuming its existence.

For developers looking for more examples of how to manipulate spreadsheets in a Laravel environment, resources like https://laravelcompany.com offer guides that bridge the gap between framework integration and PhpSpreadsheet implementation details.

Our network