如何使用PHPExcel阅读xlsx文件的第二页? [英] How do I read sheet two of an xlsx file with PHPExcel?

查看:310
本文介绍了如何使用PHPExcel阅读xlsx文件的第二页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know how to read my xlsx spreadsheet and loop through the first sheet.

它有5张纸,除第一张纸外,我都很难.

It has 5 sheets and I am having trouble getting to any other than the first.

这是我使用的代码,直接来自文档. 您可以看到我试图利用setActiveSheet,但这引发了错误Call to undefined method PHPExcel::setActiveSheet().

Here is the code I am using which was straight from the documentation. You can see I tried to utilize setActiveSheet, but that threw the error 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也可以做到,所以解决方案是这样

Ok...the names are deceiving. setActiveSheetIndex also does a get so the solution was this

$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";

这篇关于如何使用PHPExcel阅读xlsx文件的第二页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆