如何使用PHPExcel读取xlsx文件的第二张表?

时间:2010-11-13 00:51:27

标签: php phpexcel import-from-excel

我知道如何阅读我的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";

3 个答案:

答案 0 :(得分:31)

好的......这些名字都是骗人的。 setActiveSheetIndex也可以解决这个问题

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

答案 1 :(得分:14)

<?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);

}?>

答案 2 :(得分:1)

我知道答案来不及了。但我发现解决方法如下。

//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();

然后,要使用其索引访问工作表,请使用getSheet()方法。请注意,工作表从0开始索引。 例如

//Retrieve the **1st 'tab' worksheet** e.g. called 'Sheet 1'
$worksheet = $objPHPExcel->getSheet(0);

因此,在您的情况下,如果您只想阅读Sheet-2,

$worksheet = $objPHPExcel->getSheet(1);

要从Excel文件中读取所有表格,必须使用如下所示的foreach循环。

    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();*/