Extracting data (array) from named spreadsheet using phpspreadsheet lib

471 views Asked by At

I can't understand, how i can extract data (array) from named spreadsheet using phpspreadsheet lib, for ex, i have workbook with many named spreadsheet (fisrt,second etc), I get array, with all named spreadsheets:

Code:

$inputFileName = 'uploads/1.xlsx';
$inputFileType = IOFactory::identify($inputFileName);
$reader = IOFactory::createReader($inputFileType);
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load($inputFileName);
$worksheetNames = $reader->listWorksheetNames($inputFileName);

echo "<pre>";
print_r($worksheetNames);
echo "</pre>";

output:

Array
(
    [0] => firts
    [1] => second
    [2] => third
)

By using this example, I can get all data of active (first) spreadsheet:

$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
print_r($sheetData);

but how I can get for specific one, "second" spreadsheet data?

1

There are 1 answers

0
Collie-IT Anne K. Frey On

You can use

$spreadsheet->getSheetNames();

for to get a list of all sheets or you can iterate

for($i=1;$i<4;$i++){

    $spreadsheet->getSheet($i);
}

For more information, check the link here.