PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

can't open XLSX sheet, out of bounds -1

Open gauze opened this issue 9 months ago • 8 comments

PHP Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Your requested sheet index: -1 is out of bounds. The actual number of sheets is 0. in /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php:626

$ file po1326.xlsx po1326.xlsx: Microsoft Excel 2007+

^ opens ok in windows and libreoffice

$ composer show|grep spreadsheet phpoffice/phpspreadsheet 4.1.0 PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine

[code snippet] $inputFileType = 'Xlsx'; $inputFileName = DIR . '/po1326.xlsx';

$reader = IOFactory::createReader($inputFileType); $reader->setReadDataOnly(true);

$spreadsheet = $reader->load($inputFileName); $activeWorksheet = $spreadsheet->getActiveSheet(); [snip] throws error here.

I do not know what is wrong here.

thanks brian

gauze avatar Mar 02 '25 22:03 gauze

Neither do I. Can you upload your spreadsheet?

oleibman avatar Mar 03 '25 04:03 oleibman

sure thing. I think I deleted anything "incriminating"

po1326.xlsx

gauze avatar Mar 03 '25 04:03 gauze

Thank you for providing your spreadsheet. I am unable to duplicate your result with that spreadsheet and your code. Is it possible that the error is happening later than you think? What release of PhpSpreadsheet (from the above I think it's 4.1.0) and Php are you using? What environment?

My code (only minor modifications from yours):

<?php

require __DIR__ . '/PhpSpreadsheet' . '/vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

class issue4391 {
    public static function issue(): void
    {
        $inputFileType = 'Xlsx';
        $inputFileName = 'issue.4391.xlsx';
        $reader = IOFactory::createReader($inputFileType);
        $reader->setReadDataOnly(true);
        $spreadsheet = $reader->load($inputFileName);
        $activeWorksheet = $spreadsheet->getActiveSheet();
        var_dump($activeWorksheet->getCell('A7')->getValue());
    }
}

issue4391::issue();

My result:

C:\git\issue.4391.php:16:
string(3) "SEA"

oleibman avatar Mar 03 '25 16:03 oleibman

I copied your code, changed the path to vendor/autoload.php same error :S

$ php testsheet.php PHP Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Your requested sheet index: -1 is out of bounds. The actual number of sheets is 0. in /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php:626 Stack trace: #0 /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php(501): PhpOffice\PhpSpreadsheet\Spreadsheet->getSheet() #1 /home/gauze/phpspread/testsheet.php(14): PhpOffice\PhpSpreadsheet\Spreadsheet->getActiveSheet() #2 /home/gauze/phpspread/testsheet.php(19): issue4391::issue() #3 {main} thrown in /home/gauze/phpspread/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Spreadsheet.php on line 626

do my dependencies look ok? $ composer show composer/pcre 3.3.2 PCRE wrapping library that offers type-safe preg_* replacements. maennchen/zipstream-php 3.1.2 ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on... markbaker/complex 3.0.2 PHP Class for working with complex numbers markbaker/matrix 3.0.1 PHP Class for working with matrices phpoffice/phpspreadsheet 4.1.0 PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine psr/http-client 1.0.3 Common interface for HTTP clients psr/http-factory 1.1.0 PSR-17: Common interfaces for PSR-7 HTTP message factories psr/http-message 2.0 Common interface for HTTP messages psr/simple-cache 3.0.0 Common interfaces for simple caching

gauze avatar Mar 03 '25 17:03 gauze

Your dependencies match mine. I am at a loss to think of why your result should differ from mine.

oleibman avatar Mar 03 '25 19:03 oleibman

are there any environmental variables that could effect this? php?

gauze avatar Mar 03 '25 19:03 gauze

I just tried this on an Ubuntu 24.04 machine (vs Fedora 41) and no errors. some kind of Fedora problem

gauze avatar Mar 03 '25 20:03 gauze

I'm glad you found this, but it seems awfully surprising. Do you have a way of testing if you can use ZipArchive on Fedora 41 to read other zip files? (An Xlsx spreadsheet is a zip archive composed mostly of xml files.)

oleibman avatar Mar 04 '25 03:03 oleibman

No update in 5 months. Closing.

oleibman avatar Aug 04 '25 00:08 oleibman