【问题标题】:How to get row id using color code in phpexcel如何在phpexcel中使用颜色代码获取行ID
【发布时间】:2015-10-01 00:25:26
【问题描述】:

我有一个 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 以便我可以识别该行。

【问题讨论】:

  • 你试过$cell->getRow()??? @rocky
  • 是的,已经尝试过它将返回行 ID,但我面临的问题是获取颜色代码的迭代在每个大提琴上。我想通过访问每一行来获取颜色代码然后我必须创建每个颜色代码的数组

标签: php phpexcel


【解决方案1】:

经过数小时的练习,得到了我的预期效果

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

【讨论】:

    猜你喜欢
    • 2020-06-14
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    相关资源
    最近更新 更多