我們生成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 { //開源代碼Vevb.com
- $_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’);
|
新聞熱點
疑難解答