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

栏目: 编程语言 · PHP · 后端 · 发布时间: 9年前

内容简介:本文实例讲述了php生成excel列名超过26列大于Z时的解决方法。分享给大家供大家参考。具体分析如下:我们生成excel都会使用phpExcel类,这里就来给大家介绍在生成excel列名超过26列大于Z时的解决办法,这是phpExcel类中的方法,今天查到了,记录一下备忘,代码如下:

本文实例讲述了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生成excel列名超过26列大于Z时的解决方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换