如何在php中动态生成Excel列号?

问题描述 投票:0回答:7

我正在编写一个导出到 Excel 的操作,并且具有动态的列数,即在运行查询之前我不知道需要多少列,因此我需要动态分配列号,如下所示:

$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'value')
->setCellValue('A2', 'value')
->setCellValue('B2', 'value')
->setCellValue('C2', 'value')
//...etc...
->setCellValue('AA2', 'value')
->setCellValue('AB2', 'value')
->setCellValue('AC2', 'value')
//...etc...

如何使用 php 做到这一点?

更新

抱歉,列命名模式是:

A、B、C、.. AA、AB、AC ... BA、BB、BC...等,数字后缀实际上是行。不,“值实际上是从一些查询数据中填充的,设置我的值看起来更像是:

$i=1;
while($result= $query->fetch_assoc()){

->setCellValue($col.$i, $result['whatever'])
$i++;

}

我只是不知道如何让列字母以该模式递增。

php
7个回答
4
投票

最简单的答案是

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

echo Coordinate::stringFromColumnIndex(5) // return E
echo Coordinate::columnIndexFromString('AA') // returns 27 

或者您可以使用此帮助程序库来简化您的任务 https://github.com/pjp-comp/Phpspreadsheet-Extension-helper


2
投票

优化功能

function columnFromIndex($number){
    if($number === 0)
        return "A";
    $name='';
    while($number>0){
        $name=chr(65+$number%26).$name;
        $number=intval($number/26)-1;
        if($number === 0){
            $name="A".$name;
            break;
        }
    }
    return $name;
}

1
投票

也许这么晚了,我不知道是否有更好的解决方案,但是,我做了这个递归函数来解决这个问题

    private function getLastColumn($last){ //last represent how mouch X axis spaces 
                                           //you want to jump
        $max = 26; //Max characters (in number) that you cant jump, ('z´)
        $inicial = 'A';
        $col = '';
        if ($last > $max) //if last is bigger than last you should recall the function
        {
            $last = $last - $max; //discount the max to the last
            $letterAscii = ord($inicial); //tansform the charcter to to number
            $letterAscii += ($last - 1); // add last -1(-1 to stay in A to keep the 
                                         //base 1)
            $col = chr($letterAscii);    // int to ascci
            $col = $col.$this->getLastColumn($last); // recall the funcion and concat 
                                                    //the result
        }else{
            $letterAscii = ord($inicial); // same as adove
            $letterAscii += ($last - 1);
            $col = chr($letterAscii);   //only pass the result to col

        }
        
        return $col;                     //return coll
    
}

如果您需要 27 个空格才能向右移动,您将获得“AA”

如果有更好的解决方案告诉我:)


0
投票

如果您事先不知道要使用多少列/行,您怎么能指望以这种方式使用流畅的界面呢?

编辑问题后,您可能会执行以下操作:

$i = 1;
while($result = $query->fetch_assoc()){
    $col = 'A';
    foreach($result as $value) {
        $objPHPExcel->getActiveSheet()->setCellValue($col++.$i, $value);
    }
    $i++;
}

但是,您可以考虑采用另一种方法,并使用

fromArray()
方法来填充数据

$i = 1;
while($result = $query->fetch_assoc()){
    $objPHPExcel->getActiveSheet()->fromArray('A'.$i++, $result);
}

0
投票

我认为为时已晚,但对于我的片段 我做了这个功能,

function number_to_column($num) {
        $column_name = array();         
        for($i=0;$i<=$num;$i++) {
            $numeric = $i % 26;
            $letter = chr(65 + $numeric);
            $num2 = intval($num / 26);
            if ($num2 > 0) {
                if($i<26) {
                    $v_column = $letter;
                } 
                if($i>25 && $i<52) {
                    $v_column = 'A'.$letter;
                } 
                if($i>51) {
                    $v_column = 'B'.$letter;
                }
                $column_name[] = $v_column;
            } else {
                $v_column = $letter;
                $column_name[] = $v_column;
            }
        }
        return $column_name;
    }

然后我们就可以像这样使用它了

$column_name = number_to_column(count($array[0])-1); 

$column_name 将生成 Excel 行名称,


0
投票

如果您的列号从未大于 26,您可以将其简化为:

function numToExcelAlpha($n) {
    $r = 'A';
    while ($n > 1) {
        $n--;
        $r++;
    }
    return $r;
}
echo numToExcelAlpha(26);
//output = Z

0
投票

我没有找到解决方案。 如何自动生成列? (回到主要问题)

© www.soinside.com 2019 - 2024. All rights reserved.