PHP匯出Excel超過26列解決辦法

2021-08-20 07:59:48 字數 1526 閱讀 9286

最近做乙個需求,匯出excel,開始測試沒問題,當資料量大時會報錯invalid cell coordinate [1

google之後,原來當excel行超過26時,行會變成   "a[",自然無法識別.記錄踩坑過程.

將列的數字序號轉成字母使用,**如下:

phpexcel_cell::stringfromcolumnindex($i); // 從o,1,2,3,..開始,相應返回返回 a,b,c,...z,aa,ab,...

將列的字母轉成數字序號使用,**如下:

phpexcel_cell::columnindexfromstring('aa');

使用phpexcel匯出excel檔案的時候,發現報了乙個錯誤,後來查詢問題才發現是列數超過26列的問題。原先的**:

//$content是乙個需要匯出的陣列

$maxcolumn = count($content[0]);

$maxrow    = count($content);

for ($i = 0; $i < $maxcolumn; $i++) }

**中只是將列直接轉換為字母,沒有考慮到超過26列的情況,超過26列後,chr(65+$i)就變成「[」符號了。

excel行列表示方式

excel的列的表示規則從a,b,c一直到z,當超過26個字母的時候用兩個字母進行表示:aa,ab,ac...az,ba,bb,bc...bz...,當超過702時又是另外乙個種表示方法。

行的表示就是1,2,3,4,5,6,7....這樣下去。在phpexcel中要設乙個單元格的值通過setcellvalue方法就可以了,其中第乙個引數表示列和行的拼接的值,如:a1,b1,aa1,ba1這樣。

改進方法

知道這個之後,只要根據$i/26的整數部分和模部分計算出列的表示字母就可以了。當然phpexcel早就考慮到這個問題了,所以呢不用自己計算,只需要直接呼叫phpexcel_cell類中的stringfromcolumnindex方法就可以了。

/***     string from columnindex *

*     @param    int $pcolumnindex column index (base 0 !!!)

*     @return    string */

public static function stringfromcolumnindex($pcolumnindex = 0) elseif ($pcolumnindex < 702) else }

return $_indexcache[$pcolumnindex]; }

可以看出這個方法針對26列內,26到702列,超過702列都進行了處理,最後就是返回a、b、c、aa、ab這樣的字元。對一開始的錯誤**改進一下:

//$content是乙個需要匯出的陣列

$maxcolumn = count($content[0]);

$maxrow    = count($content);

for ($i = 0; $i < $maxcolumn; $i++) }

phpexcel匯出超過26列解決方案

原文 phpexcel匯出超過26列解決方案 將列的數字序號轉成字母使用,如下 phpexcel cell stringfromcolumnindex i 從o,1,2,3,開始,相應返回返回 a,b,c,z,aa,ab,將列的字母轉成數字序號使用,如下 phpexcel cell columnin...

phpexcel匯出超過26列解決方案

原文 phpexcel匯出超過26列解決方案 將列的數字序號轉成字母使用,如下 phpexcel cell stringfromcolumnindex i 從o,1,2,3,開始,相應返回返回 a,b,c,z,aa,ab,將列的字母轉成數字序號使用,如下 phpexcel cell columnin...

PHP匯出excel亂碼

php匯出excel檔案時有兩個主要的過程 1 定義檔名 2 填充excel資料 這兩個過程中可能會出現一些php匯出excel亂碼問題,下面我來說一下解決辦法 解決檔名的php匯出excel亂碼 亂碼原因 客戶使用的中文版windows系統平台,而windows平台的檔名編碼為gb2312 gbk...