這篇文章為大家帶來了關於php的相關知識,其中主要跟大家聊一聊xlswriter擴充是什麼?怎麼使用xlswriter擴充優化Excel匯出效能,有興趣的朋友下面一起來看看吧,希望對大家有幫助。
xlswriter 是一個PHP C 擴展,旨在提升php在導出大數據量時的效能問題,支援windows / Linux 。可用於在 Excel 2007 XLSX 檔案中讀取數據,插入多個工作表,寫入文字、數字、公式、日期、圖表、圖片和超連結。
它具備以下特性:
一、寫入
二、讀取
下載安裝
https://github.com/viest/php-ext-xlswriter
https://xlswriter-docs.viest.me/zh-cn/an-zhuang/huan-jing-yao-qiu
composer require viest/php-ext-xlswriter-ide-helper:dev-master
然後將裡面的幾個類別複製到一個xlswriter_ide_helper.php 檔案裡面,將這個檔案放到你的專案中就有程式碼提示了。
docker exec -it php72-fpm bashcd /usr/local/bin pecl install xlswriter docker-php-ext-enable xlswriter php -m php --ri xlswriter Version => 1.3.6 docker restart php72-fpm
private function rankPersonExport($activityInfo, $list){ $date = date('Y-m-d'); $filename = "{$activityInfo['orgname']}-{$activityInfo['name']}-个人排行榜-{$date}"; $header = ['名次', '用户ID', '对接账号', '姓名', '电话', '部门ID', '一级部门', '二级部门', '三级部门', '总积分', '最后积分时间', "毫秒"]; if (!empty($activityInfo['ext'])) { $extArr = json_decode($activityInfo['ext'], true); foreach ($extArr as $errItem) { array_push($header, $errItem['name']); } } // list $listVal = []; foreach($list as $v){ $temp = [ $v['rank'], $v['userid'], $v['userName'], $v['nickName'], $v['phone'], $v['departid'], $v['topDepartName'], $v['secDepartName'], $v['thirdDepartName'], $v['score'], $v['updatetime'], $v['micro'], ]; if (!empty($v['ext'])) { $extArr = explode('|', $v['ext']); foreach ($extArr as $k2 => $v2) { $errItemArr = explode('^', $v2); array_push($temp, $errItemArr[1]); } } array_push($listVal, $temp); } $re = downloadXLSX($filename, $header, $listVal); if($re){ return $this->output(0, $re); }else{ return $this->output(1, 'success'); }}
function getTmpDir(): string{ $tmp = ini_get('upload_tmp_dir'); if ($tmp !== False && file_exists($tmp)) { return realpath($tmp); } return realpath(sys_get_temp_dir());}/** * download xlsx file * * @param string $filename * @param array $header * @param array $list * @return string errmsg */function downloadXLSX(string $filename, array $header, array $list): string{ try { $config = ['path' => getTmpDir() . '/']; $excel = (new \Vtiful\Kernel\Excel($config))->fileName($filename.'.xlsx', 'Sheet1'); $fileHandle = $excel->getHandle(); $format1 = new \Vtiful\Kernel\Format($fileHandle); $format2 = new \Vtiful\Kernel\Format($fileHandle); // title style $titleStyle = $format1->fontSize(16) ->bold() ->font("Calibri") ->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER) ->toResource(); // global style $globalStyle = $format2->fontSize(10) ->font("Calibri") ->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER) ->border(\Vtiful\Kernel\Format::BORDER_THIN) ->toResource(); $headerLen = count($header); // header array_unshift($list, $header); // title $title = array_fill(1, $headerLen - 1, ''); $title[0] = $filename; array_unshift($list, $title); $end = strtoupper(chr(65 + $headerLen - 1)); // column style $excel->setColumn("A:{$end}", 15, $globalStyle); // title $excel->MergeCells("A1:{$end}1", $filename)->setRow("A1", 25, $titleStyle); // 冻结前两行,列不冻结 $excel->freezePanes(2, 0); // 数据 $filePath = $excel->data($list)->output(); header("Content-Disposition:attachment;filename={$filename}.xlsx"); $re = copy($filePath, 'php://output'); if ($re === false) { $err = 'failed to write output'; } else { $err = ''; } @unlink($filePath); return $err; } catch (\Vtiful\Kernel\Exception $e) { return $e->getMessage(); }}
PHP影片教學》
以上是詳解PHP用xlswriter優化Excel導出效能(附程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!