phpexcel匯出超過26列解決方案

2021-09-08 12:01:03 字數 1564 閱讀 4322

原文:phpexcel匯出超過26列解決方案

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

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...

PHP,Excel匯出換行

有id,才算真的有發票資料 if v b invoice id if int v b invoice info type 1 else v b invoice str r n公司名稱 姓名 v b invoice str v b invoice info company name v b invoi...

phpexcel 匯入匯出

匯出excel 以下是使用示例,對於以 開頭的行是不同的可選方式,請根據實際需要 開啟對應行的注釋。如果使用 excel5 輸出的內容應該是gbk編碼。require once phpexcel.php uncomment require once phpexcel writer excel5.ph...