php 使用 PHPExcel 循环工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11617077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through worksheets with PHPExcel
提问by user717236
I'm using the PHPExcel library to read an Excel file and perform processing on it. I want to loop through each worksheet. I checked the documentation and all I could find was changing the active worksheet index or only loading specified worksheets. How can I loop through all worksheets?
我正在使用 PHPExcel 库读取 Excel 文件并对其进行处理。我想遍历每个工作表。我检查了文档,我能找到的只是更改活动工作表索引或仅加载指定的工作表。如何遍历所有工作表?
Thank you for any help.
感谢您的任何帮助。
Here is the documentation's looping example, for reference:
这是文档的循环示例,供参考:
<?php
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("test.xlsx");
$objWorksheet = $objPHPExcel->getActiveSheet();
echo '<table>' . "\n";
foreach ($objWorksheet->getRowIterator() as $row) {
echo '<tr>' . "\n";
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // This loops all cells,
// even if it is not set.
// By default, only cells
// that are set will be
// iterated.
foreach ($cellIterator as $cell) {
echo '<td>' . $cell->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";
}
echo '</table>' . "\n";
?>
采纳答案by Mark Baker
You're using iterators. Did you look at the code example for iterators in the /Tests directory? If so, you might have seen reference to the WorksheetIterator
您正在使用迭代器。您是否查看了 /Tests 目录中迭代器的代码示例?如果是这样,您可能已经看到对 WorksheetIterator 的引用
Alternatively, the getAllSheets() method of the PHPExcel object returns an array of worksheets, which allows you to use a foreach loop
或者,PHPExcel 对象的 getAllSheets() 方法返回一个工作表数组,它允许您使用 foreach 循环
回答by jeffery_the_wind
I think you can do this. Increment the active sheet until there aren't any left, then do what you want with each one:
我认为你可以做到这一点。增加活动工作表,直到没有任何剩余,然后对每个工作表执行您想要的操作:
<?php
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("test.xlsx");
$i = 0;
while ($objPHPExcel->setActiveSheetIndex($i)){
$objWorksheet = $objPHPExcel->getActiveSheet();
//now do whatever you want with the active sheet
...
$i++;
}
...
?>
回答by billynoah
Here's a useful function I use for iterating over sheets and returning an array of cell values for each with the sheet title as array key:
这是一个有用的函数,我用于迭代工作表并为每个工作表返回一个单元格值数组,并将工作表标题作为数组键:
function getSheets($fileName) {
try {
$fileType = PHPExcel_IOFactory::identify($fileName);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);
$sheets = [];
foreach ($objPHPExcel->getAllSheets() as $sheet) {
$sheets[$sheet->getTitle()] = $sheet->toArray();
}
return $sheets;
} catch (Exception $e) {
die($e->getMessage());
}
}

