dqsong2010 2015-03-08 17:19
浏览 23
已采纳

从列中的某一行获取值

I currently have the below code that will loop through the excel file and return rows with missing data. the output is as below:

- Row number: 37
    - Missing : in cell B37
    - Missing : in cell D37

What I would like to return is the following

- Row number: 37
    - Missing nameFirst : in cell B37
    - Missing nameLast : in cell D37

Where nameFirst is the value of cell B1 and nameLast is the value of D1.

<?PHP
require_once 'Excel/PHPExcel.php';
require_once 'Excel/PHPExcel/Reader/Excel2007.php';
class MyReadFilter implements PHPExcel_Reader_IReadFilter
{
    public function readCell($column, $row, $worksheetName = '') {
        $Retour=false;
        $column=PHPExcel_Cell::columnIndexFromString($column);// Warning ! A=1, not zero as usual
        if($row<1 || $column>4)
            $Retour=false;
        else
            $Retour=true;
        return $Retour;
    }
}
echo '<pre>';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load('test.xlsx');
$worksheet=$objPHPExcel->getSheet(0);
foreach ($worksheet->getRowIterator() as $row) {
        if($row->getRowIndex()>1){
            echo '    - Row number: ' . $row->getRowIndex() . "
";

            $cellIterator = $row->getCellIterator();
            $cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
            foreach ($cellIterator as $cell) {
                 $colIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn());



                if($cell->getCalculatedValue() == "")
                echo '        - Missing <THIS IS WHERE I WANT THE INDEX CELL FOR THE COLUMN> :' . 'in cell ' . $cell->getCoordinate() . "
";

        }
        }
    }
echo '</pre>';

?>

I tried changing the echo to include the variable $colIndex in addition to:

            echo '        - Missing '. $worksheet->getCellByColumnAndRow($colIndex, 1)->getValue(). '. :'  . $cell->getCoordinate() . "
";

But this causes a return of one row and only one correct cell and a whole bunch of blank variables

- Row number: 2
    - Missing nameFirst. :B1
    - Missing . :E1
    - Missing . :F1
    - Missing . :G1
    - Missing . :H1
  • 写回答

1条回答 默认 最新

  • doutao4480 2015-03-08 18:06
    关注

    So you need to get the cell value for row 1 in the current cell's ($cell) column

    echo '        - Missing ' .
         $worksheet->getCell($cell->getColumn(). '1')->getValue() . 
         ' :' . 'in cell ' . 
         $cell->getCoordinate() . "
    ";
    

    should work perfectly well

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目