PHPExcel Column Loop

后端 未结 7 2020
Happy的楠姐
Happy的楠姐 2020-12-04 14:49

How can I do a loop which based on Excel worksheet columns? I found (and used) WorksheetIterator, RowIterator and CellIterator but nothing about columns.

7条回答
  •  余生分开走
    2020-12-04 15:19

    This recursive method was designed allows you to get the "letters" of the columns, whatever the number of columns you have, from "A" to "ZZZZZZZZZ..." :

    function getColumnLetter( $number )
    {
        $prefix = '';
        $suffix = '';
        $prefNum = intval( $number/26 );
        if( $prefNum > 25 )
        {
            $prefix = getColumnLetter( $prefNum );
        }
        $suffix = chr( fmod( $number, 26 )+65 );
        return $prefix.$suffix;
    }
    

    So you can loop the columns of an PHP array on index number and convert it to a string of letters and use it in PHPExcel methods, like "setCellValue" ... But solutions above are surely faster if you can use them !

提交回复
热议问题