/**
* @Author: 小破孩嫩
* @Email: 3584685883@qq.com
* @Time: 2021/8/4 14:35
* @param string $col
* @throws Exception
* @Description:根据字母获取字段列长 根据列数的最大值返回最大范围内的列值
*/
public function getColumnArrByMaxCol($col = ''){
if(empty($col)) throw new Exception("不能为空~");
if(strlen($col) > 2) throw new Exception("导入列数超出最大值~");
$col = strtoupper($col);
$column_word = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
$colArr = [];
if(in_array($col,$column_word)){
for ($i=0;$i<=25;$i++){
if($column_word[$i] == $col) {
array_push($colArr,$column_word[$i]);
return $colArr;
}
array_push($colArr,$column_word[$i]);
}
}
$firstColumn = substr($col,0,1);
$twoColumn = substr($col,1,1);
if(!in_array($firstColumn,$column_word)) throw new Exception("错误的参数");
if(!in_array($twoColumn,$column_word)) throw new Exception("错误的参数");
$firstColumnNum = array_keys($column_word,$firstColumn);
// $twoColumnNum = array_keys($column_word,$twoColumn);
$colArr = $column_word;
for($a = 0; $a <= $firstColumnNum[0]; $a++){
for($b = 0; $b <= 25; $b++){
if($twoColumn == $column_word[$b] && $firstColumnNum[0] == $a){
array_push($colArr,$column_word[$a].$column_word[$b]);
return $colArr;
}
array_push($colArr,$column_word[$a].$column_word[$b]);
}
}
}
/**
* @Author: 小破孩嫩
* @Email: 3584685883@qq.com
* @Time: 2021/9/9 13:40
* @param string $col
* @return mixed
* @throws Exception
* @Description:根据字母获取字段列长 根据列数的最大值返回最大范围内的列值 上面的方法在业务上有点慢,这里是重新改了一下
*/
public function getColumnArrByMaxColtwo($col = ''){
if(empty($col)) throw new Exception("不能为空~");
$col= strtoupper($col); //转大写
$strLength = strlen($col); //获取长度
$column_word = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; //基础数据
$column_word_vk = array_flip($column_word); //数组kv掉换
$b = $strLength;
$maxNumb = [];
//验证并计算循环次数
for($strl = 0; $strl < $strLength; $strl++){
$strAloneWord = substr($col,$strl,1);
if(!in_array($strAloneWord,$column_word)){
throw new Exception("错误的参数");
}
$columnNumber[] = $column_word_vk[$strAloneWord]+1;
$maxNum = pow(26,$b-$strl-1);
$maxNumb[$strl] = $maxNum*$columnNumber[$strl];
}
$totolTimes = ceil(array_sum($maxNumb)/26);//向上取整 循环次数
//数据拼接 限制在两位字母最大ZZ
for($c = 0; $c < $totolTimes; $c++) {
$first_word = $column_word[$c-1];
foreach($column_word as $key => $val){
if($c >= 1){
$word = $first_word.$column_word[$key];
}else{
$word = $column_word[$key];
}
$column[] = $word;
}
}
for($a = 0; $a < array_sum($maxNumb); $a++){
$new_column[] = $column[$a];
}
return $new_column;
}
版权属于:
小破孩
作品采用:
《
署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
》许可协议授权
评论