Home > Backend Development > PHP Tutorial > PHP generates excel column names, more than 26 columns are larger than Z problem solution_PHP tutorial

PHP generates excel column names, more than 26 columns are larger than Z problem solution_PHP tutorial

WBOY
Release: 2016-07-13 10:46:51
Original
876 people have browsed it

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.

To convert the numeric serial number of the column into letters use:
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’);

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’);

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template