Dmitriy Klochko
Dmitriy Klochko

Reputation: 51

How read merged and cells with background color in PHPExcel

I have function:

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;
  }

How can I use the following code?

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

For work with merged cells inside my code. Currently my function add value only for first cell, and all an others is empty.

Thank you.

Upvotes: 2

Views: 348

Answers (1)

Dmitriy Klochko
Dmitriy Klochko

Reputation: 51

Found an answer for my issue:

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;
  }

Upvotes: 0

Related Questions