PHP SpreadSheet-将col +行转换为单元格

问题描述 投票:1回答:1

我正在尝试使用PHP Spreadsheet,并且我希望将col + row转换为range。例如:col-> 1和row-> 1 =“ A1”

示例:

$worksheet2->getCellByColumnAndRow($col, $row)->getValue());

至:

$worksheet2->getCellValue(¿?);

如何从$ col和$ row变量转换为range(Ex:“ A1”)?

谢谢!

php excel spreadsheet
1个回答
0
投票

您在这里:

function colLetter($c, $offset=null){
   // https://icesquare.com/wordpress/example-code-to-convert-a-number-to-excel- 
   //column-letter/
   //0 -> A
   //25 -> Z
   //26 -> AA
   //27 -> AB
   //799 -> ADT

    if ( is_null($offset) ) { //converts position 0 to 2 etc etc.
        $offset = 0;
    }
    $offset += 1;  //the solution converts 1 -> A, hence we need to add 1 if we want to start with zero

    $c = (int)$c;
    if ($c < 0) {
        return '';
    } else {
        $c += $offset;
    }
    $letter = '';             
    while($c != 0){
       $p = ($c - 1) % 26;
       $c = (int)(($c - $p) / 26);
       $letter = chr(65 + $p) . $letter;
    }    
    return $letter;        
}

这会在D列的$ key + 3列中写入内容(如果$ key为0):

$xC = colLetter( (int)$key, 3 );        
$ws->setCellValue( $xC . '2', $date )
   ->setCellValue( $xC . '444', $datedmy );
© www.soinside.com 2019 - 2024. All rights reserved.