PhpSpreadsheet foreach 循环遍历多个工作表

标签 php excel phpspreadsheet

我是 PhpSpreadsheet 的新手,我有一个包含多个工作表的文件(全部相同),我检查了 Reader 中的所有示例文档部分,但每个示例都以类似的代码结尾

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

看来我只能从事件工作表中获取数据。 我想循环遍历每张纸以从每张纸中获取数据,例如:

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

有什么想法吗?我错过了什么吗? 谢谢

最佳答案

您需要使用 getSheetCount() 方法来确定有多少个工作表,然后使用带有 getSheet() 的标准 for 循环:

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

请参阅Worksheet documentation .

关于PhpSpreadsheet foreach 循环遍历多个工作表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52007144/

相关文章:

PHPOffice 写入新文件时出现 fatal error

javascript - 如何在网页加载时自动提交表单?

php - 如何在 Laravel Eloquent Collection 数组前加上键值对?

php - 如何根据条件从数据库加载

PhpSpreadsheet - 获取 d/m/Y 日期的原始字符串值,而不是转换为 float

php - 使用 PhpSpreadsheet 编写 xlsx 文件会删除所有文本框

php - 如何向数据库表中的多个收件人发送电子邮件?

sql - 如何解析数据库中每个单元格的特定值?

excel - 在 Excel 中计算概率并绘制 cdf

Python - 任务计划程序 0x1