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

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

内容简介:本文实例讲述了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 Four

The Four

Scott Galloway / Portfolio / 2017-10-3 / USD 28.00

NEW YORK TIMES BESTSELLER USA TODAY BESTSELLER Amazon, Apple, Facebook, and Google are the four most influential companies on the planet. Just about everyone thinks they know how they got there.......一起来看看 《The Four》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具

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

在线XML、JSON转换工具