您的位置 首页 php

php如何设置excel列数超过A-Z?

一般php处理A-Z的列数可以用

 $i=97;
for($data as $row=>$item){
foreach($item as $value){
    $sheet->setCellValue(chr($i).($row+1),$value);
   }
}  

但是超过了Z后呢?且无限大呢?

    function getExcelColumnKey(int $index)
    {
        $keyCodes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $ offset  = intval(floor($index / 26));
        $colCode = $offset > 0 ? $keyCodes[--$offset] : '';
        return $colCode . $keyCodes[$index % 26];
    }  
 for($data as $row=>$item){
  $colIndex=0;
foreach($item as $value){
    $sheet->setCellValue(getExcelColumnKey($colIndex++).($row+1),$value);
   }
}  

若有更好的方法,欢迎大家可以留言讨论!

文章来源:智云一二三科技

文章标题:php如何设置excel列数超过A-Z?

文章地址:https://www.zhihuclub.com/78032.shtml

关于作者: 智云科技

热门文章

网站地图