We will use the phpExcel class when generating excel. Now let me introduce to you the solution to the problem of generating excel column names with more than 26 columns and larger than Z.
This is a method in the phpExcel class. I found it today and took a note.
The code is as follows
代码如下 |
复制代码 |
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];
}
|
|
Copy code
|
代码如下 |
复制代码 |
PHPExcel_Cell::stringFromColumnIndex($i); // 从o开始
|
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::columnIndexFromString(‘AA’);
|
|
To convert the numeric serial number of the column into letters use:
The code is as follows
|
Copy code
|
PHPExcel_Cell::stringFromColumnIndex($i); // Start with o
To convert the letters of the column into numerical serial numbers, use:
The code is as follows
|
Copy code
|
PHPExcel_Cell::columnIndexFromString(‘AA’);
http://www.bkjia.com/PHPjc/632927.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632927.htmlTechArticleWe will use the phpExcel class to generate excel. Let me introduce to you how to generate excel column names with more than 26 columns. Solution to problem Z. This is the method in phpExcel class. I found it today...
|
|