Php generates an excel column name. more than 26 columns are greater than Z. _ PHP Tutorial

Source: Internet
Author: User
Php generates an excel column name, with more than 26 columns greater than Z. solution. All the generated excel files use the phpExcel class. next I will introduce the solution to the problem that the generated excel column name contains more than 26 columns and is greater than Z. This is a method in the phpExcel class. Today we found that all the generated excel files use the phpExcel class. next I will introduce the solution to the problem of generating more than 26 columns of the excel column name than Z.

This is a method in the phpExcel class. I found it today and recorded my memo.

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];
}

Convert the number of a column to a letter:

The code is as follows:

PHPExcel_Cell: stringFromColumnIndex ($ I); // starts from o

To convert a column's letters into numbers:

The code is as follows:

PHPExcel_Cell: columnIndexFromString ('A ');

Bytes. This is a method in the phpExcel class. Found today...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.