gpt4 book ai didi

php - 如何使用 PHPExcel 读取 xlsx 文件的第二页?

转载 作者:IT王子 更新时间:2023-10-28 23:45:18 28 4
gpt4 key购买 nike

我知道如何阅读我的 xlsx 电子表格并循环浏览第一张表格。

它有 5 张纸,除了第一张以外,我很难找到其他任何一张。

这是我使用的直接来自文档的代码。你可以看到我尝试使用 setActiveSheet,但是抛出了错误Call to undefined method PHPExcel::setActiveSheet()

代码:

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("cmt_school_data.xlsx");
//$objPHPExcel->setActiveSheet(1);
$objWorksheet = $objPHPExcel->getActiveSheet();

echo '<table border=1>' . "\n";

foreach ($objWorksheet->getRowIterator() as $row) {

echo '<tr>' . "\n";

$cellIterator = $row->getCellIterator();

// This loops all cells, even if it is not set.
// By default, only cells that are set will be iterated.
$cellIterator->setIterateOnlyExistingCells(false);

foreach ($cellIterator as $cell) {
echo '<td>' . $cell->getValue() . '</td>' . "\n";
}

echo '</tr>' . "\n";

}

echo '</table>' . "\n";

最佳答案

好吧……名字是骗人的。 setActiveSheetIndex 也执行 get 所以解决方案是这样的

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("cmt_school_data.xlsx");
$objWorksheet = $objPHPExcel->setActiveSheetIndex(1);
//objWorksheet = $objPHPExcel->getActiveSheet();
echo '<table border=1>' . "\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";

关于php - 如何使用 PHPExcel 读取 xlsx 文件的第二页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4170275/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com