一般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);
}
}
若有更好的方法,欢迎大家可以留言讨论!