当前位置: 移动技术网 > IT编程>开发语言>PHP > php生成excel列名超过26列大于Z时的解决方法

php生成excel列名超过26列大于Z时的解决方法

2018年07月08日  | 移动技术网IT编程  | 我要评论

北京万达影城影讯,内科病例分析题,双桥区二手房

本文实例讲述了php生成excel列名超过26列大于z时的解决方法。分享给大家供大家参考。具体分析如下:

我们生成excel都会使用phpexcel类,这里就来给大家介绍在生成excel列名超过26列大于z时的解决办法,这是phpexcel类中的方法,今天查到了,记录一下备忘,代码如下:

复制代码 代码如下:
public static function stringfromcolumnindex($pcolumnindex = 0) 

        //  using a lookup cache adds a slight memory overhead, but boosts speed 
        //  caching using a static within the method is faster than a class static, 
        //      though it's additional memory overhead 
        static $_indexcache = array(); 
  
        if (!isset($_indexcache[$pcolumnindex])) { 
            // determine column string 
            if ($pcolumnindex < 26) { 
                $_indexcache[$pcolumnindex] = chr(65 + $pcolumnindex); 
            } elseif ($pcolumnindex < 702) { 
                $_indexcache[$pcolumnindex] = chr(64 + ($pcolumnindex / 26)) . chr(65 + $pcolumnindex % 26); 
            } else {
                $_indexcache[$pcolumnindex] = chr(64 + (($pcolumnindex - 26) / 676)) . chr(65 + ((($pcolumnindex - 26) % 676) / 26)) . chr(65 + $pcolumnindex % 26); 
            } 
        } 
        return $_indexcache[$pcolumnindex]; 
}

将列的数字序号转成字母使用,代码如下:
复制代码 代码如下:
phpexcel_cell::stringfromcolumnindex($i); // 从o开始

将列的字母转成数字序号使用,代码如下:

复制代码 代码如下:
phpexcel_cell::columnindexfromstring('aa');

希望本文所述对大家的php程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网