如何使用phpexcel中的颜色代码获取行id

时间:2015-09-30 08:10:47

标签: php phpexcel

我有一个excel文件,其中包含某些颜色的行我想要获取特定颜色代码的行ID但无法执行此操作..已经搜索过,但下面没有找到我的代码PHPEXCEL

$cellColor = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();

这将为我提供颜色代码和我$cell->getValue()的值,其中$cell$cellIterator的某个变量

  foreach ($worksheet->getRowIterator() as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false); 

           foreach ($cellIterator as $cell) 
            {
              $cellColor = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();

            if (!empty($cell->getCalculatedValue())) {
                   if ($cellColor == 'yellow') { 
                       echo ($cellColor.'======'.$cell->getValue());
                   }
               }
            }
   }

$cell->getValue()会给我特定颜色代码的值
但是,问题是如果我有2行颜色为黄色,那么$cell->getValue()会给出两个值,如
0-> yellow1
1-> yellow2
但是在删除excel中的第一个黄色数据后,结果将是0-> yellow2
这是错的需要是0->''
1-> yellow2这就是为什么我需要特定颜色的行ID以便我可以识别该行。

1 个答案:

答案 0 :(得分:0)

经过几个小时的练习后得到了我的预期结果

$objPHPExcel = PHPExcel_IOFactory::load('someFile.xls');
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {

    //$worksheetTitle = $worksheet->getTitle();
    $highestRow = $worksheet->getHighestRow();
    $highestColumn = $worksheet->getHighestColumn();
    $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
    //$nrColumns = ord($highestColumn) - 64;

    $groupCount = array();
    $standardSetCount = array();
    $standardCount = array();
    $learningTargetCount = array();

    for ($row = 1; $row <= $highestRow; ++$row) {
        for ($col = 0; $col < $highestColumnIndex; ++$col) {
            $cell = $worksheet->getCellByColumnAndRow($col, $row);
            $colorCode = $objPHPExcel->getActiveSheet()->getStyle($cell->getCoordinate())->getFill()->getStartColor()->getRGB();

            /*
             * Yellow 
             */

            if ($colorCode == 'FFFF00') {
                $val = $cell->getValue();
                //$dataType = PHPExcel_Cell_DataType::dataTypeForValue($val);
                $groupCount[] = $val;   // $groupCount[] = $dataType;
            }

            /*
             * gold
             */

            if ($colorCode == 'CC9900') {
                $val = $cell->getValue();
                $standardSetCount[] = $val;
            }

            /*
             * red
             */

            if ($colorCode == 'FF3333') {
                $val = $cell->getValue();
                $standardCount[] = $val;
            }

            /*
             * green
             */

            if ($colorCode == '00CC33') {
                $val = $cell->getValue();
                $learningTargetCount[] = $val;
            }
        }
    }

    $group = (array_chunk($groupCount, $highestColumnIndex));
    $standardSet = (array_chunk($standardSetCount, $highestColumnIndex));
    $standard = (array_chunk($standardCount, $highestColumnIndex));
    $learningTarget = (array_chunk($learningTargetCount, $highestColumnIndex));

    echo '<pre>';
    print_r($learningTarget);
}