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使用curl实现简单模拟提交表单功能

php使用curl实现简单模拟提交表单功能

php 使用curl 进行简单模拟提交表单,供大家参考,具体内容如下 //初始化curl $ch = curl_init(); $url = 'xxx'; $option = [...

利用客户端缓存对网站进行优化的原理分析第1/2页

很多人首先会想从服务器缓存方面着手对程序进行优化,许多不同的服务器缓存方式都有他们自己的特点,像我曾经参与的一些项目中,根据缓存的命中率不同使用过 Com+/Enterprise Lib...

PHP 采集心得技巧

1.获取远程文件源代码(file_get_contents或用fopen). 2.分析代码得到自己想要的内容(这里用正规匹配,一般是得到分页)。 3.跟根得到的内容进行下载入库等操作。...

shopex主机报错误请求解决方案(No such file or directory)

一、shopex主机环境 1、windows 2003 R2 2、iis6.0+php5.0以上 3、mysql5.0以上 如果有希望了解php环境搭配的,请查阅: windows200...

php获取$_POST同名参数数组的实现介绍

今天写php的时候发现$_POST["arr"]无法获取参数arr的数组,记录一下。例如有以下表单需要提交:复制代码 代码如下:  <input type="checkbox" n...