如何使用 PHPExcel 读取 xlsx 文件的第二页?
我知道如何阅读我的 xlsx 电子表格并循环浏览第一张表格。
它有 5 张,除了第一张之外,我很难找到其他任何一张。
这是我正在使用的代码,直接来自文档。 您可以看到我尝试使用 setActiveSheet,但这引发了错误调用未定义的方法 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";
I know how to read my xlsx spreadsheet and loop through the first sheet.
It has 5 sheets and I am having trouble getting to any other than the first.
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()
.
Code:
$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";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧……这些名字都是骗人的。 setActiveSheetIndex 也执行 get 操作,所以解决方案是这样的
Ok...the names are deceiving. setActiveSheetIndex also does a get so the solution was this
我知道现在回答已经太晚了。但我找到解决方案如下。
然后,要通过索引访问工作表,请使用 getSheet() 方法。请注意,工作表的索引从 0 开始。
例如
,在您的情况下,如果您只想读取 Sheet-2,
或者从 Excel 文件中读取所有工作表,则必须使用 foreach 循环,如下所示。
I know it's been too late for the answer. But I find solution as below.
Then, to access a sheet by its index, use the getSheet() method. Note that sheets are indexed from 0.
e.g.
So in your case, if you want to read only Sheet-2 then,
OR to read all Sheets from your Excel file, you have to use foreach loop as below.
如何使用 PHPExcel 读取单个 Excel 文件中的多个工作表。 [ 使用 Codeigniter 3.1.11 ]
来自单个 excel 文件的多个工作表
1:https://i.sstatic.net/KyPQz.png
源代码
// 批量上传
函数批量上传()
{
$输出=数组();
How do I read multiple worksheet from a single excel file using PHPExcel. [ using Codeigniter 3.1.11 ]
multiple worksheet from a single excel file
1: https://i.sstatic.net/KyPQz.png
Source Code
// Bulk upload
function bulk_upload()
{
$output = array();