如何在PHPExcel中读取合并的和具有背景颜色的单元格

时间:2019-01-16 08:06:52

标签: php phpexcel phpexcelreader phpexcel-1.8.0

我有功能:

function funct($xls){
    include_once '../system/PHPExcel/Classes/PHPExcel/IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load($xls);
    $objPHPExcel->setActiveSheetIndex(9);

    $aSheet = $objPHPExcel->getActiveSheet();
     $array = array();

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

      $cellIterator = $row->getCellIterator();
    //this array will contain arrays with cells values
      $item = array();
 foreach($cellIterator as $key => $cell){
//skip adding tables which we don't need
if($key == "A" || $key == "E" || $key == "F" || $key == "G" || $key == 'H') continue;

        array_push($item, $cell->getCalculatedValue());
      }
      array_push($array, $item);
    }
    return $array;
  }

如何使用以下代码?

  $this->mergedCellsRange = $this->activeSheet->getMergeCells();
foreach($this->mergedCellsRange as $currMergedRange) {
if($cell->isInRange($currMergedRange)) {
    $currMergedCellsArray = PHPExcel_Cell::splitRange($currMergedRange);
        $cell = $this->activeSheet->getCell($currMergedCellsArray[0][0]);
        break;
    }
}

用于处理代码中的合并单元格。 目前,我的函数仅为第一个单元格添加值,其他所有单元格均为空。

谢谢。

1 个答案:

答案 0 :(得分:0)

找到了我的问题的答案:

function funct($xls){
    include_once '../system/PHPExcel/Classes/PHPExcel/IOFactory.php';
    $objPHPExcel = PHPExcel_IOFactory::load($xls);
    $objPHPExcel->setActiveSheetIndex(9);

    $aSheet = $objPHPExcel->getActiveSheet();
     $array = array();

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

      $cellIterator = $row->getCellIterator();
    //this array will contain arrays with cells values
      $item = array();
       foreach($cellIterator as $key => $cell){
//merged cells echo start
     foreach ($aSheet->getMergeCells() as $cells) {
if ($cell->isInRange($cells)) {
$currMergedCellsArray = PHPExcel_Cell::splitRange($cells);
$cell = $aSheet->getCell($currMergedCellsArray[0][0]);
echo $cells,'\n';
    echo '';
    break;
}
}
//merged cells echo end
//skip adding tables which we don't need
if($key == "A" || $key == "E" || $key == "F" || $key == "G" || $key == 'H') continue;

        array_push($item, $cell->getCalculatedValue());
      }
      array_push($array, $item);
    }
    return $array;
  }