我們產生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程式設計有所協助。