新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
PHPSpreadsheet導出Excel列數(shù)超過26報錯怎么辦?
PHPSpreadsheet導出Excel列數(shù)超過26報錯怎么辦?下面本篇文章給大家介紹一下PhpSpreadsheet導出Excel超過26列解決辦法,希望對大家有幫助。

使用PhpSpreadsheet導出excel文件的時候,發(fā)現(xiàn)報了一個錯誤,后來查詢問題才發(fā)現(xiàn)是列數(shù)超過26列的問題,下面看看解決方法。
excel 行列表示方式
-
xexcel 的列的表示規(guī)則從 A,B,C 一直到 Z,當超過 26 個字母的時候用兩個字母進行表示:AA,AB,AC…AZ,BA,BB,BC…BZ…,當超過 702 時又是另外一個種表示方法。
-
行的表示就是 1,2,3,4,5,6,7…. 這樣下去。在 phpexcel 中要設一個單元格的值通過 setCellValue 方法就可以了,其中第一個參數(shù)表示列和行的拼接的值,如:A1,B1,AA1,BA1 這樣。
知道這個之后,只要根據(jù) $i/26 的整數(shù)部分和模部分計算出列的表示字母就可以了。
下面是解決方法
data_type= $data_type;
$this->spread_sheet= $spread_sheet;
$this->x_lsx= $x_lsx;
}
/**
* @ description 文件導出
* @ date 2019-05-06
* @ array $field_name 字段名稱 漢字(索引數(shù)組) ['產(chǎn)品','姓名']
* @ array $data 數(shù)據(jù) ['a' => data, 'b' => data]
* @ array $field_column 數(shù)據(jù)中的下標名稱 字段數(shù)據(jù) (索引數(shù)組) ['a','b']
* @ string $file_name 文件名稱
* @ array $arr 需要轉(zhuǎn)換為數(shù)字的$field_column中的key(索引數(shù)組)
* @ return file
*/
public function export($field_name,$data,$field_column,$file_name,$field_numeric_keys= [])
{
@ob_clean();
if (empty($field_name)|| empty($field_column)|| empty($data)|| empty($file_name))return false;
$sheet= $this->spread_sheet->getActiveSheet();
//設置header
$i= 0;
foreach ($field_name as $value) {
$cellName= self::stringFromColumnIndex($i). "1";
$sheet->setCellValue($cellName, $value)->calculateColumnWidths();
$sheet->getColumnDimension(self::stringFromColumnIndex($i))->setWidth(15);
$i++;
}
//設置value
$len= count($field_column);
foreach ($data as $key=> $item) {
$row= 2 + ($key* 1);
for ($i= 0; $i< $len; $i++) {
$sheet->setCellValueExplicit(self::stringFromColumnIndex($i). $row, $item[$field_column[$i]]??"", DataType::TYPE_STRING);
if (isset($item[$field_column[$i]])&& !empty($field_numeric_keys)&& in_array($field_column[$i],$field_numeric_keys)) {
$sheet->setCellValueExplicit(self::stringFromColumnIndex($i). $row, $item[$field_column[$i]]??"", DataType::TYPE_NUMERIC);
}
}
}
$writer= new Xlsx($this->spread_sheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');//告訴瀏覽器輸出07Excel文件
header('Content-Disposition: attachment;filename="' . $file_name . '.xlsx"');//告訴瀏覽器輸出瀏覽器名稱
header('Cache-Control: max-age=0');
$writer->save('php://output');
@ob_flush();
@flush();
exit;
}
/**
* @ description excel導出突破只能26個字段
* @ date 2019-05-07
* @ return string 字段
*/
public static function stringFromColumnIndex($pColumnIndex = 0)
{
static $_indexCache= array();
if (!isset($_indexCache[$pColumnIndex])) {
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];
}
} 文章標題:PHPSpreadsheet導出Excel列數(shù)超過26報錯怎么辦?
網(wǎng)址分享:http://m.fisionsoft.com.cn/article/djhgiig.html


咨詢
建站咨詢
