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";
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";
<?php
/** Include path **/
set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/');
/** PHPExcel_IOFactory */
include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
// $inputFileType = 'Excel2007';
// $inputFileType = 'Excel2003XML';
// $inputFileType = 'OOCalc';
// $inputFileType = 'Gnumeric';
$inputFileName = './sampleData/example1.xls';
echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />';
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
echo 'Loading all WorkSheets<br />';
$objReader->setLoadAllSheets();
$objPHPExcel = $objReader->load($inputFileName);
echo '<hr />';
echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />';
$loadedSheetNames = $objPHPExcel->getSheetNames();
foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) {
echo **$sheetIndex**,' -> ',$loadedSheetName,'<br />';
$sheetData = $objPHPExcel->**getSheet**(**$sheetIndex**)->toArray(null,true,true,true);
var_dump($sheetData);
}?>
I know it's been too late for the answer. But I find solution as below.
//load library - EXCEL
$this->load->library('excel');
$objPHPExcel = PHPExcel_IOFactory::load('./folder/exceldata.xls');
//Get How Many Sheets in your Excel file.
echo $objPHPExcel->getSheetCount();
Then, to access a sheet by its index, use the getSheet() method. Note that sheets are indexed from 0. e.g.
//Retrieve the **1st 'tab' worksheet** e.g. called 'Sheet 1'
$worksheet = $objPHPExcel->getSheet(0);
So in your case, if you want to read only Sheet-2 then,
$worksheet = $objPHPExcel->getSheet(1);
OR to read all Sheets from your Excel file, you have to use foreach loop as below.
foreach($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
//echo $highestRow;
//echo $highestColumn;
//die();
for($row=2; $row<=$highestRow; $row++)
{
$column1 = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$column2= $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$finaldata[] = array(
'data1' => trim($column1),
'data2' => trim($column2),
);
}
}
/*echo "<pre>";
echo count($finaldata);
print_r($finaldata);
die();*/
来源:https://stackoverflow.com/questions/4170275/how-do-i-read-sheet-two-of-an-xlsx-file-with-phpexcel