PHPExcel Check if sheet exists

You can check if a sheet exists by name with the method sheetNameExists($pSheetName).


If you simply want to know whether a sheetexists at index 1, then

$sheetCount = $excel->getSheetCount();

will return a count of the worksheets. As sheets are indexed incrementally from 0, then a sheet at index 1 will only exist if the count is 2 or more.

If you want to know whether a named sheet exists, then

$sheetNames = $excel->getSheetNames();

will return an array of sheet names (indexed by their index position), and you can then test using in_array();

The

$excel->getSheet()

method will throw an exception if the requested sheet (by index) doesn't exist, so wrap it in a try/catch block would be another approach

$excel->getSheetByName()

returns a NULL value if the named worksheet doesn't exist


getSheet($sheetNumber) is how you check if a sheet exists.

Tags:

Php

Phpexcel