Phpexcel export more than 26 columns of solutions

Source: Internet
Author: User

Original: Phpexcel export more than 26 columns of solutions

To convert a column's numeric number to a letter, use the following code:

Phpexcel_cell::stringfromcolumnindex ($i);//From o,1,2,3,.. Start, return return a,b,c,... Z,aa,ab,...

To convert the letters of a column to a numeric sequence number, use the following code:

  phpexcel_cell::columnindexfromstring (' AA ');   when I exported an Excel file using Phpexcel, I found an error and later queried the problem only to find out that the number of columns exceeded 26 columns. The original code:  //$content is an array that needs to be exported $maxcolumn = count ($content [0]), $maxRow    =count ($content); for ($i = 0; $i & Lt $maxColumn; $i + +) {    for ($j = 0; $j < $maxRow; $j + +) {       $pCoordinate = chr (65+ $i). ‘‘ . ($j + 1);       $pValue     = $content [$j] [$i];      $ Objphpexcel->getactivesheet ()->setcellvalue ($pCoordinate, $pValue);   }} The code simply converts the column directly to a letter, Without taking into account more than 26 columns, Chr (65+ $i) becomes a "[" symbol after more than 26 columns.  excel the column representation of the  excel is represented by a rule from a,b,c to Z, when more than 26 letters are represented by two letters: Aa,ab,ac ... AZ,BA,BB,BC ... BZ ..., when more than 702 is another method of expression. The expression of the   line is 1,2,3,4,5,6,7 .... Go on like this. The value of a cell in Phpexcel is done by means of the Setcellvalue method, where the first parameter represents the concatenation of columns and rows, such as: A1,b1,aa1,ba1.   Improvement method   Knowing this, you can just calculate the letter of the out-of-line representation based on the integer and modulo portions of $I/26. Of course, Phpexcel has long considered this problem, so do not have to calculate their own, just call the Phpexcel_cell class directly in the Stringfromcolumnindex method can be.  /** *    string from columnindex * *     @param    int $pColumnIndexColumn index (base 0!!!)  *     @return    string */public static function Stringfromcolumnindex ($pColumnIndex =0) {   //  using a lookup cache adds a slight memoryoverhead, but boosts speed   //&NBSP;&N Bsp;caching using a static within the method Isfaster than a class static,   //      thoug H it ' sadditional memory overhead    static $_indexcache =array ();     if (!isset ($_indexcache[$ Pcolumnindex]) {      //Determine column string       if ($pColumnIndex < 26 ) {          $_indexcache[$pColumnIndex] = chr (+ $pColumnIndex);     &nbs P } elseif ($pColumnIndex < 702) {          $_indexcache[$pColumnIndex] = Chr ($pColum NINDEX/26) .              &NBSP;  chr (+ $pColumnIndex%);      } else {         &NBSP;$_INDEXCAC he[$pColumnIndex] = Chr (+ ($pColumnIndex –26)/676) .              &NBSP;&NB Sp Chr ((($pColumnIndex –26)% 676) .                 chr (65 + $pColumnIndex%);      }   }    return$_indexcache[$pColumnIndex];} It can be seen that this method for 26 columns, 26 to 702 columns, more than 702 columns are processed, and finally return a, B, C, AA, AB and other characters. Improve the error code at the beginning:  //$content is an array that needs to be exported $maxcolumn = count ($content [0]), $maxRow    =count ($content); for ( $i = 0; $i < $maxColumn; $i + +) {    for ($j = 0; $j < $maxRow; $j + +) {       $pCoordinate =phpexcel_cell::stringfro Mcolumnindex ($i). ‘‘ . ($j + 1);       $pValue     = $content [$j] [$i];      $ Objphpexcel->getactivesheet ()->setcellvalue ($pCoordinatE, $pValue);   }}   from:http://www.01happy.com/phpexcel-column-more-than-26/

Phpexcel export more than 26 columns of solutions

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.