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

yipeiwu_com6年前PHP代码库

本文实例讲述了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程序设计有所帮助。

相关文章

PHP排序算法之希尔排序(Shell Sort)实例分析

PHP排序算法之希尔排序(Shell Sort)实例分析

本文实例讲述了PHP排序算法之希尔排序(Shell Sort)。分享给大家供大家参考,具体如下: 基本思想: 希尔排序是指记录按下标的一定增量分组,对每一组使用 直接插入排序 ,随着增量...

PHP实现搜索相似图片

感知哈希算法 count < =5 匹配最相似 count > 10 两张不同的图片 var_dump(ImageHash::run(‘./1.png', ‘./psb.jp...

php 生成饼图 三维饼图

php 生成饼图 三维饼图

饼图 复制代码 代码如下: <?php //+------------------------+ //| pie3dfun.PHP//公用函数 | //+-------------...

也谈php网站在线人数统计

  function checkOnline($userid,$tempid=null)       {&n...

php校验表单检测字段是否为空的方法

本文实例讲述了php校验表单检测字段是否为空的方法。分享给大家供大家参考。具体如下: php校验表单,检测字段是否为空,当表单中有未填写的字段,则会显示错误信息。 <html&...