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

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

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

查看所有标签

猜你喜欢:

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

High Performance Python

High Performance Python

Micha Gorelick、Ian Ozsvald / O'Reilly Media / 2014-9-10 / USD 39.99

If you're an experienced Python programmer, High Performance Python will guide you through the various routes of code optimization. You'll learn how to use smarter algorithms and leverage peripheral t......一起来看看 《High Performance Python》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

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

UNIX 时间戳转换