PhpSpreadsheet foreach loop through multiple sheets

泪湿孤枕 提交于 2020-05-29 05:03:44

问题


I'm new to PhpSpreadsheet, I have a file with multiple sheets (all the same), I checked all the examples in the Reader section of the documentation, but each example ends with a code like

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

so seems that I can get my data only from the active sheet. I would like to loop through each sheet to get data from each one, something like:

foreach ($sheetData as $sheet) { 
    echo "...my data ...";
}

Any idea? Am I missing something? Thanks


回答1:


You'd want to use the getSheetCount() method to determine how many sheets there are, and then use a standard for loop with getSheet():

$sheetCount = $spreadsheet->getSheetCount();
for ($i = 0; $i < $sheetCount; $i++) {
    $sheet = $spreadsheet->getSheet($i);
    $sheetData = $sheet->toArray(null, true, true, true);
}

See the Worksheet documentation.



来源:https://stackoverflow.com/questions/52007144/phpspreadsheet-foreach-loop-through-multiple-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!